Skip to content

Commit b92908b

Browse files
committed
Adds structured change format support
1 parent 793d8bd commit b92908b

File tree

5 files changed

+420
-64
lines changed

5 files changed

+420
-64
lines changed

KustoSchemaTools/Changes/DatabaseScriptContainer.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using KustoSchemaTools.Model;
2+
using Newtonsoft.Json;
23

34
namespace KustoSchemaTools.Changes
45
{
@@ -23,11 +24,22 @@ public DatabaseScriptContainer(string kind, int order, string script, bool isAsy
2324
IsAsync = isAsync;
2425
}
2526

27+
[JsonProperty("script")]
2628
public DatabaseScript Script { get; set; }
27-
public string Kind{ get; set; }
29+
30+
[JsonProperty("kind")]
31+
public string Kind { get; set; }
32+
33+
[JsonProperty("isValid")]
2834
public bool? IsValid { get; set; }
35+
36+
[JsonProperty("text")]
2937
public string Text => Script.Text;
38+
39+
[JsonProperty("order")]
3040
public int Order => Script.Order;
31-
public bool IsAsync { get;set; }
41+
42+
[JsonProperty("isAsync")]
43+
public bool IsAsync { get; set; }
3244
}
3345
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using KustoSchemaTools.Model;
5+
6+
namespace KustoSchemaTools.Changes
7+
{
8+
public static class StructuredChangeExtensions
9+
{
10+
public static StructuredChange ToStructuredChange(this IChange change)
11+
{
12+
if (change == null)
13+
{
14+
throw new ArgumentNullException(nameof(change));
15+
}
16+
17+
var structuredChange = new StructuredChange
18+
{
19+
EntityType = change.EntityType,
20+
Entity = change.Entity,
21+
Scripts = change.Scripts?.Select(CloneScript).ToList() ?? new List<DatabaseScriptContainer>(),
22+
Comment = StructuredComment.From(change.Comment)
23+
};
24+
25+
switch (change)
26+
{
27+
case Heading heading:
28+
structuredChange.ChangeType = "Heading";
29+
structuredChange.HeadingText = heading.Entity;
30+
structuredChange.Scripts.Clear();
31+
break;
32+
case DeletionChange deletion:
33+
structuredChange.ChangeType = "Delete";
34+
structuredChange.DeletedEntities = new List<string> { deletion.Entity };
35+
break;
36+
case ScriptCompareChange scriptCompare:
37+
structuredChange.ChangeType = scriptCompare.From == null ? "Create" : "Update";
38+
structuredChange.ScriptComparison = scriptCompare.ToStructuredScriptComparison();
39+
break;
40+
default:
41+
structuredChange.ChangeType = "Update";
42+
break;
43+
}
44+
45+
structuredChange.DeletedEntities ??= new List<string>();
46+
47+
return structuredChange;
48+
}
49+
50+
private static StructuredScriptComparison? ToStructuredScriptComparison(this ScriptCompareChange change)
51+
{
52+
var comparison = new StructuredScriptComparison
53+
{
54+
NewScripts = change.Scripts?.Select(CloneScript).ToList() ?? new List<DatabaseScriptContainer>()
55+
};
56+
57+
if (change.From != null)
58+
{
59+
var previousScripts = change.From
60+
.CreateScripts(change.Entity, false)
61+
.GroupBy(script => script.Kind)
62+
.Select(group => group.First())
63+
.ToDictionary(script => script.Kind, script => script);
64+
65+
foreach (var script in comparison.NewScripts)
66+
{
67+
if (previousScripts.TryGetValue(script.Kind, out var previous))
68+
{
69+
comparison.OldScripts.Add(CloneScript(previous));
70+
}
71+
}
72+
}
73+
74+
foreach (var script in comparison.OldScripts.Where(s => !s.IsValid.HasValue))
75+
{
76+
script.IsValid = true;
77+
}
78+
79+
return comparison;
80+
}
81+
82+
private static DatabaseScriptContainer CloneScript(DatabaseScriptContainer source)
83+
{
84+
var clone = new DatabaseScriptContainer(new DatabaseScript(source.Script.Text, source.Script.Order), source.Kind, source.IsAsync)
85+
{
86+
IsValid = source.IsValid
87+
};
88+
89+
return clone;
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)