Skip to content

Commit

Permalink
Merge pull request #75 from github/caol-ila-fix-creating-invalid-tables
Browse files Browse the repository at this point in the history
add validation rule for table creation
  • Loading branch information
caol-ila authored Jun 20, 2024
2 parents 9084c41 + 01f0889 commit 36b8834
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions KustoSchemaTools/Changes/DatabaseChanges.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using KustoSchemaTools.Model;
using Microsoft.Extensions.Logging;
using System.Data;

namespace KustoSchemaTools.Changes
{
Expand Down Expand Up @@ -43,7 +44,7 @@ public static List<IChange> GenerateChanges(Database oldState, Database newState

result.AddRange(GenerateDeletions(oldState, newState.Deletions, log));

result.AddRange(GenerateScriptCompareChanges(oldState, newState, db => db.Tables, nameof(newState.Tables), log));
result.AddRange(GenerateScriptCompareChanges(oldState, newState, db => db.Tables, nameof(newState.Tables), log, (oldItem, newItem) => oldItem != null || newItem.Columns?.Any() == true));
result.AddRange(GenerateScriptCompareChanges(oldState, newState, db => db.MaterializedViews, nameof(newState.MaterializedViews), log));
result.AddRange(GenerateScriptCompareChanges(oldState, newState, db => db.ContinuousExports, nameof(newState.ContinuousExports), log));
result.AddRange(GenerateScriptCompareChanges(oldState, newState, db => db.Functions, nameof(newState.Functions), log));
Expand Down Expand Up @@ -132,7 +133,9 @@ private static List<IChange> GenerateEntityGroupChanges(Database oldState, Datab
return changes;
}

private static List<IChange> GenerateScriptCompareChanges<T>(Database oldState, Database newState,Func<Database,Dictionary<string,T>> entitySelector,string entityName, ILogger log) where T: IKustoBaseEntity


private static List<IChange> GenerateScriptCompareChanges<T>(Database oldState, Database newState,Func<Database,Dictionary<string,T>> entitySelector,string entityName, ILogger log, Func<T?,T,bool> validator = null) where T: IKustoBaseEntity
{
var tmp = new List<IChange>();
var existing = entitySelector(oldState) ?? new Dictionary<string, T>();
Expand All @@ -143,6 +146,12 @@ private static List<IChange> GenerateScriptCompareChanges<T>(Database oldState,

foreach (var item in newItems)
{
var existingOldItem = existing.ContainsKey(item.Key) ? existing[item.Key] : default(T);
if(validator != null && !validator(existingOldItem, item.Value))
{
log.LogInformation($"Skipping {entityName} {item.Key} as it failed validation");
continue;
}
if (existing.ContainsKey(item.Key))
{
var change = new ScriptCompareChange(item.Key, existing[item.Key], item.Value);
Expand Down

0 comments on commit 36b8834

Please sign in to comment.