|
1 | | -using KustoSchemaTools.Model; |
| 1 | +using Kusto.Language; |
| 2 | +using KustoSchemaTools.Model; |
2 | 3 | using Microsoft.Extensions.Logging; |
3 | 4 | using System.Data; |
| 5 | +using System.Text; |
4 | 6 |
|
5 | 7 | namespace KustoSchemaTools.Changes |
6 | 8 | { |
@@ -175,6 +177,124 @@ private static List<IChange> GenerateScriptCompareChanges<T>(Database oldState, |
175 | 177 |
|
176 | 178 | return tmp; |
177 | 179 | } |
| 180 | + |
| 181 | + public static List<IChange> GenerateFollowerChanges(FollowerDatabase oldState, FollowerDatabase newState, ILogger log) |
| 182 | + { |
| 183 | + List<IChange> result = |
| 184 | + [ |
| 185 | + .. GenerateFollowerCachingChanges(oldState, newState, db => db.Tables, "Table", "table"), |
| 186 | + .. GenerateFollowerCachingChanges(oldState, newState, db => db.MaterializedViews, "MV", "materialized-view"), |
| 187 | + |
| 188 | + ]; |
| 189 | + |
| 190 | + if (oldState.Permissions.ModificationKind != newState.Permissions.ModificationKind) |
| 191 | + { |
| 192 | + var kind = newState.Permissions.ModificationKind.ToString().ToLower(); |
| 193 | + result.Add(new BasicChange("FollowerDatabase", "PermissionsModificationKind", $" Change Permission-Modification-Kind from {oldState.Permissions.ModificationKind} to {newState.Permissions.ModificationKind}", new List<DatabaseScriptContainer> |
| 194 | + { |
| 195 | + new DatabaseScriptContainer(new DatabaseScript($".alter follower database {newState.DatabaseName} principals-modification-kind = {kind}", 0), "FollowerChangePolicyModificationKind") |
| 196 | + })); |
| 197 | + } |
| 198 | + if (oldState.Cache.ModificationKind != newState.Cache.ModificationKind) |
| 199 | + { |
| 200 | + var kind = newState.Cache.ModificationKind.ToString().ToLower(); |
| 201 | + result.Add(new BasicChange("FollowerDatabase", "ChangeModificationKind", $"Change Caching-Modification-Kind from {oldState.Cache.ModificationKind} to {newState.Cache.ModificationKind}", new List<DatabaseScriptContainer> |
| 202 | + { |
| 203 | + new DatabaseScriptContainer(new DatabaseScript($".alter follower database {newState.DatabaseName} caching-policies-modification-kind = {kind}", 0), "FollowerChangePolicyModificationKind") |
| 204 | + })); |
| 205 | + } |
| 206 | + |
| 207 | + if (oldState.Cache.DefaultHotCache != newState.Cache.DefaultHotCache) |
| 208 | + { |
| 209 | + if (newState.Cache.DefaultHotCache != null) |
| 210 | + { |
| 211 | + result.Add(new BasicChange("FollowerDatabase", "ChangeDefaultHotCache", $"From {oldState.Cache.DefaultHotCache} to {newState.Cache.DefaultHotCache}", new List<DatabaseScriptContainer> |
| 212 | + { |
| 213 | + new DatabaseScriptContainer(new DatabaseScript($".alter follower database {newState.DatabaseName} policy caching hot = {newState.Cache.DefaultHotCache}", 0), "FollowerChangeDefaultHotCache") |
| 214 | + })); |
| 215 | + } |
| 216 | + else |
| 217 | + { |
| 218 | + result.Add(new BasicChange("FollowerDatabase", "DeleteDefaultHotCache", $"Remove Default Hot Cache", new List<DatabaseScriptContainer> |
| 219 | + { |
| 220 | + new DatabaseScriptContainer(new DatabaseScript($".delete follower database {newState.DatabaseName} policy caching", 0), "FollowerDeleteDefaultHotCache") |
| 221 | + })); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + foreach(var script in result.SelectMany(itm => itm.Scripts)) |
| 226 | + { |
| 227 | + var code = KustoCode.Parse(script.Text); |
| 228 | + var diagnostics = code.GetDiagnostics(); |
| 229 | + script.IsValid = diagnostics.Any() == false; |
| 230 | + } |
| 231 | + |
| 232 | + return result; |
| 233 | + |
| 234 | + } |
| 235 | + |
| 236 | + private static List<IChange> GenerateFollowerCachingChanges(FollowerDatabase oldState, FollowerDatabase newState, Func<FollowerCache, Dictionary<string,string>> selector, string type, string kustoType) |
| 237 | + { |
| 238 | + var result = new List<IChange>(); |
| 239 | + var oldEntities = selector(oldState.Cache); |
| 240 | + var newEntities = selector(newState.Cache); |
| 241 | + |
| 242 | + |
| 243 | + var removedPolicyScripts = oldEntities.Keys.Except(newEntities.Keys) |
| 244 | + .Select(itm => |
| 245 | + new |
| 246 | + { |
| 247 | + Name = itm, |
| 248 | + Script = new DatabaseScriptContainer(new DatabaseScript($".delete follower database {newState.DatabaseName} {kustoType} {itm} policy caching", 0), $"FollowerDelete{type}CachingPolicies") |
| 249 | + }) |
| 250 | + .ToList(); |
| 251 | + var changedPolicyScripts = newEntities |
| 252 | + .Where(itm => oldEntities.ContainsKey(itm.Key) == false |
| 253 | + || oldEntities[itm.Key] != itm.Value) |
| 254 | + .Select(itm => new |
| 255 | + { |
| 256 | + Name = itm.Key, |
| 257 | + From = oldEntities.ContainsKey(itm.Key) ? oldEntities[itm.Key] : "default", |
| 258 | + To = itm.Value, |
| 259 | + Script = new DatabaseScriptContainer(new DatabaseScript($".alter follower database {newState.DatabaseName} {kustoType} {itm.Key} policy caching hot = {itm.Value}", 0), $"FollowerChange{type}CachingPolicies") |
| 260 | + }) |
| 261 | + .ToList(); |
| 262 | + |
| 263 | + if (removedPolicyScripts.Any()) |
| 264 | + { |
| 265 | + var deletePolicySb = new StringBuilder(); |
| 266 | + deletePolicySb.AppendLine($"## Delete {type} Caching Policies"); |
| 267 | + foreach (var change in removedPolicyScripts) |
| 268 | + { |
| 269 | + deletePolicySb.AppendLine($"* {change.Name}"); |
| 270 | + } |
| 271 | + |
| 272 | + result.Add(new BasicChange( |
| 273 | + "FollowerDatabase", |
| 274 | + $"Delete{type}CachingPolicy", |
| 275 | + deletePolicySb.ToString(), |
| 276 | + removedPolicyScripts.Select(itm => itm.Script).ToList())); |
| 277 | + } |
| 278 | + if (changedPolicyScripts.Any()) |
| 279 | + { |
| 280 | + var changePolicies = new StringBuilder(); |
| 281 | + changePolicies.AppendLine($"## Changed {type} Caching Policies"); |
| 282 | + changePolicies.AppendLine($"{type} | From | To"); |
| 283 | + changePolicies.AppendLine("--|--|--"); |
| 284 | + foreach (var change in changedPolicyScripts) |
| 285 | + { |
| 286 | + changePolicies.AppendLine($"{change.Name} | {change.From} | {change.To}"); |
| 287 | + } |
| 288 | + |
| 289 | + result.Add(new BasicChange( |
| 290 | + "FollowerDatabase", |
| 291 | + $"Change{type}CachingPolicy", |
| 292 | + changePolicies.ToString(), |
| 293 | + changedPolicyScripts.Select(itm => itm.Script).ToList())); |
| 294 | + } |
| 295 | + |
| 296 | + return result; |
| 297 | + } |
178 | 298 | } |
179 | 299 |
|
180 | 300 | } |
0 commit comments