|
| 1 | +using CmdScale.EntityFrameworkCore.TimescaleDB.Operations; |
| 2 | +using Microsoft.EntityFrameworkCore.Infrastructure; |
| 3 | +using System.Globalization; |
| 4 | + |
| 5 | +namespace CmdScale.EntityFrameworkCore.TimescaleDB.Design.Generators |
| 6 | +{ |
| 7 | + internal class ReorderPolicyOperationGenerator |
| 8 | + { |
| 9 | + public static void Generate(AddReorderPolicyOperation operation, IndentedStringBuilder builder) |
| 10 | + { |
| 11 | + List<string> statements = |
| 12 | + [ |
| 13 | + BuildAddReorderPolicySql(operation.TableName, operation.IndexName, operation.InitialStart) |
| 14 | + ]; |
| 15 | + |
| 16 | + List<string> alterJobClauses = BuildAlterJobClauses(operation); |
| 17 | + if (alterJobClauses.Count != 0) |
| 18 | + { |
| 19 | + statements.Add(BuildAlterJobSql(operation.TableName, alterJobClauses)); |
| 20 | + } |
| 21 | + |
| 22 | + MigrationBuilderSqlHelper.BuildQueryString(statements, builder); |
| 23 | + } |
| 24 | + |
| 25 | + public static void Generate(AlterReorderPolicyOperation operation, IndentedStringBuilder builder) |
| 26 | + { |
| 27 | + List<string> statements = []; |
| 28 | + bool needsRecreation = operation.IndexName != operation.OldIndexName || operation.InitialStart != operation.OldInitialStart; |
| 29 | + List<string> alterJobClauses = BuildAlterJobClauses(operation); |
| 30 | + |
| 31 | + if (needsRecreation) |
| 32 | + { |
| 33 | + statements.Add($"SELECT remove_reorder_policy('\"\"{operation.TableName}\"\"', if_exists => true);"); |
| 34 | + statements.Add(BuildAddReorderPolicySql(operation.TableName, operation.IndexName, operation.InitialStart)); |
| 35 | + |
| 36 | + if (alterJobClauses.Count != 0) |
| 37 | + { |
| 38 | + statements.Add(BuildAlterJobSql(operation.TableName, alterJobClauses)); |
| 39 | + } |
| 40 | + } |
| 41 | + else if (alterJobClauses.Count != 0) |
| 42 | + { |
| 43 | + statements.Add(BuildAlterJobSql(operation.TableName, alterJobClauses)); |
| 44 | + } |
| 45 | + |
| 46 | + MigrationBuilderSqlHelper.BuildQueryString(statements, builder); |
| 47 | + } |
| 48 | + |
| 49 | + public static void Generate(DropReorderPolicyOperation operation, IndentedStringBuilder builder) |
| 50 | + { |
| 51 | + List<string> statements = |
| 52 | + [ |
| 53 | + $"SELECT remove_reorder_policy('\"\"{operation.TableName}\"\"', if_exists => true);" |
| 54 | + ]; |
| 55 | + MigrationBuilderSqlHelper.BuildQueryString(statements, builder); |
| 56 | + } |
| 57 | + |
| 58 | + private static List<string> BuildAlterJobClauses(AddReorderPolicyOperation operation) |
| 59 | + { |
| 60 | + List<string> clauses = []; |
| 61 | + if (operation.ScheduleInterval != DefaultValues.ReorderPolicyScheduleInterval) |
| 62 | + clauses.Add($"schedule_interval => INTERVAL '{operation.ScheduleInterval}'"); |
| 63 | + |
| 64 | + if (operation.MaxRuntime != null) // Default is null (no limit) |
| 65 | + clauses.Add($"max_runtime => INTERVAL '{operation.MaxRuntime}'"); |
| 66 | + |
| 67 | + if (operation.MaxRetries != DefaultValues.ReorderPolicyMaxRetries) |
| 68 | + clauses.Add($"max_retries => {operation.MaxRetries}"); |
| 69 | + |
| 70 | + if (operation.RetryPeriod != DefaultValues.ReorderPolicyRetryPeriod) |
| 71 | + clauses.Add($"retry_period => INTERVAL '{operation.RetryPeriod}'"); |
| 72 | + |
| 73 | + return clauses; |
| 74 | + } |
| 75 | + |
| 76 | + private static List<string> BuildAlterJobClauses(AlterReorderPolicyOperation operation) |
| 77 | + { |
| 78 | + List<string> clauses = []; |
| 79 | + |
| 80 | + if (operation.ScheduleInterval != operation.OldScheduleInterval) |
| 81 | + clauses.Add($"schedule_interval => INTERVAL '{operation.ScheduleInterval}'"); |
| 82 | + |
| 83 | + if (operation.MaxRuntime != operation.OldMaxRuntime) |
| 84 | + { |
| 85 | + string maxRuntimeValue = string.IsNullOrWhiteSpace(operation.MaxRuntime) ? "NULL" : $"INTERVAL '{operation.MaxRuntime}'"; |
| 86 | + clauses.Add($"max_runtime => {maxRuntimeValue}"); |
| 87 | + } |
| 88 | + |
| 89 | + if (operation.MaxRetries != operation.OldMaxRetries) |
| 90 | + clauses.Add($"max_retries => {operation.MaxRetries}"); |
| 91 | + |
| 92 | + if (operation.RetryPeriod != operation.OldRetryPeriod) |
| 93 | + clauses.Add($"retry_period => INTERVAL '{operation.RetryPeriod}'"); |
| 94 | + |
| 95 | + return clauses; |
| 96 | + } |
| 97 | + |
| 98 | + private static string BuildAlterJobSql(string tableName, IEnumerable<string> clauses) |
| 99 | + { |
| 100 | + return $@" |
| 101 | +SELECT alter_job(job_id, {string.Join(", ", clauses)}) |
| 102 | +FROM timescaledb_information.jobs |
| 103 | +WHERE proc_name = 'policy_reorder' AND hypertable_name = '{tableName}';".Trim(); |
| 104 | + } |
| 105 | + |
| 106 | + private static string BuildAddReorderPolicySql(string tableName, string indexName, DateTime? initialStart) |
| 107 | + { |
| 108 | + string baseSql = $"SELECT add_reorder_policy('\"\"{tableName}\"\"', '{indexName}'"; |
| 109 | + |
| 110 | + List<string> optionalArgs = []; |
| 111 | + |
| 112 | + // Add optional arguments if they are provided |
| 113 | + if (initialStart.HasValue) |
| 114 | + { |
| 115 | + // Use ISO 8601 format for timestamps to avoid ambiguity |
| 116 | + string timestamp = initialStart.Value.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture); |
| 117 | + optionalArgs.Add($"initial_start => '{timestamp}'"); |
| 118 | + } |
| 119 | + |
| 120 | + if (optionalArgs.Count > 0) |
| 121 | + { |
| 122 | + baseSql += $", {string.Join(", ", optionalArgs)}"; |
| 123 | + } |
| 124 | + |
| 125 | + baseSql += ");"; |
| 126 | + return baseSql; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments