Skip to content

Commit fea30c6

Browse files
authored
Merge pull request #127 from github/feature/structured_diff
Add initial support for structured diffs
2 parents 793d8bd + 67da8f1 commit fea30c6

16 files changed

+659
-108
lines changed

KustoSchemaTools.Tests/Parser/YamlDatabaseHandlerTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async Task GetDatabase()
2121
.WithPlugin(new TablePlugin())
2222
.WithPlugin(new FunctionPlugin())
2323
.WithPlugin(new DatabaseCleanup());
24-
var loader = factory.Create(Path.Combine(BasePath, Deployment), Database);
24+
var loader = factory.Create(Path.Join(BasePath, Deployment), Database);
2525

2626
var db = await loader.LoadAsync();
2727

@@ -51,7 +51,7 @@ public async Task VerifyFunctionPreformatted()
5151
.WithPlugin(new TablePlugin())
5252
.WithPlugin(new FunctionPlugin());
5353
// DatabaseCleanup intentionally omitted
54-
var loaderWithoutCleanup = factoryWithoutCleanup.Create(Path.Combine(BasePath, Deployment), Database);
54+
var loaderWithoutCleanup = factoryWithoutCleanup.Create(Path.Join(BasePath, Deployment), Database);
5555
var dbWithoutCleanup = await loaderWithoutCleanup.LoadAsync();
5656

5757
// with the DatabaseCleanup plugin
@@ -60,7 +60,7 @@ public async Task VerifyFunctionPreformatted()
6060
.WithPlugin(new FunctionPlugin())
6161
.WithPlugin(new MaterializedViewsPlugin())
6262
.WithPlugin(new DatabaseCleanup());
63-
var loaderWithCleanup = factoryWithCleanup.Create(Path.Combine(BasePath, Deployment), Database);
63+
var loaderWithCleanup = factoryWithCleanup.Create(Path.Join(BasePath, Deployment), Database);
6464
var dbWithCleanup = await loaderWithCleanup.LoadAsync();
6565

6666
// Assert
@@ -113,15 +113,15 @@ public async Task VerifyMaterializedView()
113113
.WithPlugin(new TablePlugin())
114114
.WithPlugin(new MaterializedViewsPlugin());
115115
// DatabaseCleanup intentionally omitted
116-
var loaderWithoutCleanup = factoryWithoutCleanup.Create(Path.Combine(BasePath, Deployment), Database);
116+
var loaderWithoutCleanup = factoryWithoutCleanup.Create(Path.Join(BasePath, Deployment), Database);
117117
var dbWithoutCleanup = await loaderWithoutCleanup.LoadAsync();
118118

119119
// with the DatabaseCleanup plugin
120120
var factoryWithCleanup = new YamlDatabaseHandlerFactory<Model.Database>()
121121
.WithPlugin(new TablePlugin())
122122
.WithPlugin(new MaterializedViewsPlugin())
123123
.WithPlugin(new DatabaseCleanup());
124-
var loaderWithCleanup = factoryWithCleanup.Create(Path.Combine(BasePath, Deployment), Database);
124+
var loaderWithCleanup = factoryWithCleanup.Create(Path.Join(BasePath, Deployment), Database);
125125
var dbWithCleanup = await loaderWithCleanup.LoadAsync();
126126

127127
// Assert
@@ -158,7 +158,7 @@ public async Task VerifyFunctionWithCommentAtEnd()
158158
.WithPlugin(new TablePlugin())
159159
.WithPlugin(new FunctionPlugin())
160160
.WithPlugin(new DatabaseCleanup());
161-
var loader = factory.Create(Path.Combine(BasePath, Deployment), Database);
161+
var loader = factory.Create(Path.Join(BasePath, Deployment), Database);
162162

163163
// Act - Load the database
164164
var db = await loader.LoadAsync();

KustoSchemaTools/Changes/ClusterChanges.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,17 @@ public static ClusterChangeSet GenerateChanges(Cluster oldCluster, Cluster newCl
4141
// Run Kusto code diagnostics
4242
foreach (var script in changeSet.Scripts)
4343
{
44-
var code = KustoCode.Parse(script.Text);
44+
var code = KustoCode.Parse(script.Script.Text);
4545
var diagnostics = code.GetDiagnostics();
4646
script.IsValid = !diagnostics.Any();
47+
script.Diagnostics = diagnostics.Any()
48+
? diagnostics.Select(diagnostic => new ScriptDiagnostic
49+
{
50+
Start = diagnostic.Start,
51+
End = diagnostic.End,
52+
Description = diagnostic.Description
53+
}).ToList()
54+
: null;
4755
}
4856

4957
changeSet.Markdown = GenerateClusterMarkdown(changeSet);
@@ -244,7 +252,7 @@ private static string GenerateClusterMarkdown(ClusterChangeSet changeSet)
244252
sb.AppendLine("```kql");
245253
foreach (var script in changeSet.Scripts)
246254
{
247-
sb.AppendLine(script.Text);
255+
sb.AppendLine(script.Script.Text);
248256
sb.AppendLine();
249257
}
250258
sb.AppendLine("```");

KustoSchemaTools/Changes/DatabaseChanges.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,17 @@ .. GenerateFollowerCachingChanges(oldState, newState, db => db.MaterializedViews
261261

262262
foreach(var script in result.SelectMany(itm => itm.Scripts))
263263
{
264-
var code = KustoCode.Parse(script.Text);
264+
var code = KustoCode.Parse(script.Script.Text);
265265
var diagnostics = code.GetDiagnostics();
266-
script.IsValid = diagnostics.Any() == false;
266+
script.IsValid = !diagnostics.Any();
267+
script.Diagnostics = diagnostics.Any()
268+
? diagnostics.Select(diagnostic => new ScriptDiagnostic
269+
{
270+
Start = diagnostic.Start,
271+
End = diagnostic.End,
272+
Description = diagnostic.Description
273+
}).ToList()
274+
: null;
267275
}
268276

269277
return result;

KustoSchemaTools/Changes/DatabaseScriptContainer.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using KustoSchemaTools.Model;
1+
using System.Collections.Generic;
2+
using KustoSchemaTools.Model;
3+
using Newtonsoft.Json;
24

35
namespace KustoSchemaTools.Changes
46
{
@@ -23,11 +25,19 @@ public DatabaseScriptContainer(string kind, int order, string script, bool isAsy
2325
IsAsync = isAsync;
2426
}
2527

28+
[JsonProperty("script")]
2629
public DatabaseScript Script { get; set; }
27-
public string Kind{ get; set; }
30+
31+
[JsonProperty("kind")]
32+
public string Kind { get; set; }
33+
34+
[JsonProperty("isValid")]
2835
public bool? IsValid { get; set; }
29-
public string Text => Script.Text;
30-
public int Order => Script.Order;
31-
public bool IsAsync { get;set; }
36+
37+
[JsonProperty("isAsync")]
38+
public bool IsAsync { get; set; }
39+
40+
[JsonProperty("diagnostics", NullValueHandling = NullValueHandling.Ignore)]
41+
public List<ScriptDiagnostic>? Diagnostics { get; set; }
3242
}
3343
}

KustoSchemaTools/Changes/DeletionChange.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using KustoSchemaTools.Parser;
1+
using KustoSchemaTools.Model;
2+
using KustoSchemaTools.Parser;
23
using System.Text;
4+
using System.Linq;
35
using Kusto.Language;
46

57

@@ -22,9 +24,17 @@ public List<DatabaseScriptContainer> Scripts
2224
get
2325
{
2426
var sc = new DatabaseScriptContainer("Deletion", 0, $".drop {EntityType} {Entity}");
25-
var code = KustoCode.Parse(sc.Text);
27+
var code = KustoCode.Parse(sc.Script.Text);
2628
var diagnostics = code.GetDiagnostics();
27-
sc.IsValid = diagnostics.Any() == false;
29+
sc.IsValid = !diagnostics.Any();
30+
sc.Diagnostics = diagnostics.Any()
31+
? diagnostics.Select(diagnostic => new ScriptDiagnostic
32+
{
33+
Start = diagnostic.Start,
34+
End = diagnostic.End,
35+
Description = diagnostic.Description
36+
}).ToList()
37+
: null;
2838
return new List<DatabaseScriptContainer> { sc };
2939
}
3040
}

KustoSchemaTools/Changes/EntityGroupChange.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,23 @@ private void Init()
2828
var toEntityArr = string.Join(",", toEntityStrings);
2929
var toScript = new DatabaseScriptContainer("EntityGroup", 3, $".create-or-alter entity_group {Entity} ({toEntityArr})");
3030

31-
if (added.Any() == false && removed.Any() == false)
31+
if (!added.Any() && !removed.Any())
3232
{
3333
return;
3434
}
3535

3636
Scripts.Add(toScript);
37-
var code = KustoCode.Parse(toScript.Text);
37+
var code = KustoCode.Parse(toScript.Script.Text);
3838
var diagnostics = code.GetDiagnostics();
39-
toScript.IsValid = diagnostics.Any() == false;
39+
toScript.IsValid = !diagnostics.Any();
40+
toScript.Diagnostics = diagnostics.Any()
41+
? diagnostics.Select(diagnostic => new ScriptDiagnostic
42+
{
43+
Start = diagnostic.Start,
44+
End = diagnostic.End,
45+
Description = diagnostic.Description
46+
}).ToList()
47+
: null;
4048
var logo = toScript.IsValid.Value ? ":green_circle:" : ":red_circle:";
4149

4250

KustoSchemaTools/Changes/ScriptCompareChange.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Data;
88
using DiffPlex.DiffBuilder.Model;
99
using Kusto.Language.Editor;
10+
using System.Linq;
1011

1112
namespace KustoSchemaTools.Changes
1213
{
@@ -23,7 +24,7 @@ private void Init()
2324
var to = To.CreateScripts(Entity, From == null);
2425
Markdown = string.Empty;
2526

26-
if (to.Any() == false) return;
27+
if (!to.Any()) return;
2728

2829
StringBuilder sb = new StringBuilder($"## {Entity}");
2930
sb.AppendLine();
@@ -32,8 +33,8 @@ private void Init()
3233
foreach (var change in to)
3334
{
3435
var before = from.ContainsKey(change.Kind) ? from[change.Kind] : null;
35-
var beforeText = before?.Text ?? "";
36-
var afterText = change.Text;
36+
var beforeText = before?.Script.Text ?? string.Empty;
37+
var afterText = change.Script.Text;
3738

3839
var singleLinebeforeText = new KustoCodeService(KustoCode.Parse(beforeText)).GetMinimalText(MinimalTextKind.SingleLine);
3940
var singleLineafterText = new KustoCodeService(KustoCode.Parse(afterText)).GetMinimalText(MinimalTextKind.SingleLine);
@@ -43,18 +44,27 @@ private void Init()
4344

4445
var zipped = reducedBefore.GetLexicalTokens().Zip(reducedAfter.GetLexicalTokens()).ToList();
4546
var diffs = zipped.Where(itm => itm.First.Text != itm.Second.Text).ToList();
46-
if(diffs.Any() == false) continue;
47+
if (!diffs.Any()) continue;
4748

4849
if (singleLinebeforeText.Equals(singleLineafterText)) continue;
4950

5051
var differ = new Differ();
5152
var diff = InlineDiffBuilder.Diff(beforeText, afterText, true);
5253
if (diff.Lines.All(itm => itm.Type == ChangeType.Unchanged)) continue;
5354

54-
var code = KustoCode.Parse(change.Text);
55+
var code = KustoCode.Parse(change.Script.Text);
5556

5657
var diagnostics = code.GetDiagnostics();
57-
change.IsValid = diagnostics.Any() == false || change.Order == -1;
58+
var hasDiagnostics = diagnostics.Any();
59+
change.IsValid = !hasDiagnostics || change.Script.Order == -1;
60+
change.Diagnostics = hasDiagnostics
61+
? diagnostics.Select(diagnostic => new ScriptDiagnostic
62+
{
63+
Start = diagnostic.Start,
64+
End = diagnostic.End,
65+
Description = diagnostic.Description
66+
}).ToList()
67+
: null;
5868
Scripts.Add(change);
5969

6070

@@ -102,10 +112,10 @@ private void Init()
102112
}
103113
sb.AppendLine("<tr>");
104114
sb.AppendLine($" <td colspan=\"2\">Script:</td>");
105-
sb.AppendLine($" <td colspan=\"10\"><pre lang=\"kql\">{change.Text.PrettifyKql()}</pre></td>");
115+
sb.AppendLine($" <td colspan=\"10\"><pre lang=\"kql\">{change.Script.Text.PrettifyKql()}</pre></td>");
106116
sb.AppendLine("</tr>");
107117

108-
if (change.IsValid == false)
118+
if (change.IsValid is false)
109119
{
110120
foreach (var diagnostic in diagnostics)
111121
{

0 commit comments

Comments
 (0)