Skip to content

Commit 823cf8a

Browse files
committed
pr updates
1 parent e77450b commit 823cf8a

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

KustoSchemaTools.Tests/DefaultDatabaseWriterTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ static IChange CreateMockChange(string name)
118118

119119
static ScriptExecuteCommandResult CreateMockResult(IChange change, bool isSuccess)
120120
{
121-
return new ScriptExecuteCommandResult {
121+
return new ScriptExecuteCommandResult
122+
{
122123
OperationId = Guid.NewGuid(),
123124
CommandType = "Script",
124125
Result = isSuccess ? "Completed" : "Failed",
@@ -178,7 +179,8 @@ internal override Task<List<ScriptExecuteCommandResult>> ApplyChangesToDatabase(
178179
{
179180
ApplyChangesToDatabaseCallCount++;
180181
var results = new List<ScriptExecuteCommandResult>();
181-
foreach (var c in changes) {
182+
foreach (var c in changes)
183+
{
182184
var result = ResultsCache[c];
183185
results.Add(result);
184186
}
@@ -227,4 +229,4 @@ public void Dispose() { }
227229
}
228230
#endregion
229231
}
230-
}
232+
}

KustoSchemaTools/Parser/KustoWriter/DefaultDatabaseWriter.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,18 @@ public virtual async Task WriteAsync(Database sourceDb, Database targetDb, Kusto
3131
}
3232
}
3333

34+
/// <summary>
35+
/// Iteratively generates and applies changes to the primary database until it stops making forward progress.
36+
/// </summary>
37+
/// <param name="sourceDb"></param>
38+
/// <param name="targetDb"></param>
39+
/// <param name="client"></param>
40+
/// <param name="logger"></param>
41+
/// <returns></returns>
3442
internal virtual async Task<List<ScriptExecuteCommandResult>> UpdatePrimary(Database sourceDb, Database targetDb, KustoClient client, ILogger logger)
3543
{
3644
// Some changes will be dependent upon each other, causing a race condition when attempting to apply them.
37-
// As long as the write made some forward progress, keep looping until it stalls.
45+
// As long as the write made some forward progress, keep looping until there are no more successes.
3846
logger.LogInformation($"Updating primary database {targetDb.Name}");
3947
var keepGoing = false;
4048
var iterationCount = 0;
@@ -56,17 +64,19 @@ internal virtual async Task<List<ScriptExecuteCommandResult>> UpdatePrimary(Data
5664
keepGoing = successes.Count < results.Count && successes.Count > 0;
5765
if (!keepGoing)
5866
{
59-
// add remaining results to the list.
67+
// if we're stopping, add remaining (failure) results to the list.
6068
allResults.AddRange(results);
6169
}
6270
} while (keepGoing);
6371

6472
// Final status
65-
Console.WriteLine($"Successfully applied {allResults.Count(r => r.Result != "Failed")} out of {allResults.Count} changes to {targetDb.Name}");
73+
Console.WriteLine("---------------------------------------------------------------------------");
74+
Console.WriteLine($"Database update complete: Successfully applied {allResults.Count(r => r.Result != "Failed")} out of {allResults.Count} changes to {targetDb.Name} after {iterationCount} iterations.");
6675
foreach (var result in allResults)
6776
{
6877
Console.WriteLine($"{result.CommandType} ({result.OperationId}): {result.Result} => {result.Reason} ({result.CommandText})");
6978
}
79+
Console.WriteLine("---------------------------------------------------------------------------");
7080

7181
return allResults;
7282
}
@@ -161,7 +171,7 @@ internal virtual async Task<List<ScriptExecuteCommandResult>> ApplyChangesToData
161171
return results;
162172
}
163173

164-
internal virtual async Task<ScriptExecuteCommandResult> ExecuteAsyncCommand(string databaseName, KustoClient client, ILogger logger, DatabaseScriptContainer sc)
174+
private static async Task<ScriptExecuteCommandResult> ExecuteAsyncCommand(string databaseName, KustoClient client, ILogger logger, DatabaseScriptContainer sc)
165175
{
166176
var interval = TimeSpan.FromSeconds(5);
167177
var iterations = (int)(TimeSpan.FromHours(1) / interval);
@@ -193,11 +203,13 @@ internal virtual async Task<ScriptExecuteCommandResult> ExecuteAsyncCommand(stri
193203
throw new Exception("Operation did not complete in a reasonable time");
194204
}
195205

196-
internal virtual async Task<List<ScriptExecuteCommandResult>> ExecutePendingSync(
206+
/// <summary>
207+
/// This function will build a single .execute script with(ContinueOnErrors = true) using all the scripts provided.
208+
/// Execute script is defined here: https://learn.microsoft.com/en-us/kusto/management/execute-database-script?view=azure-data-explorer
209+
/// </summary>
210+
private static async Task<List<ScriptExecuteCommandResult>> ExecutePendingSync(
197211
string databaseName, KustoClient client, ILogger logger, List<DatabaseScriptContainer> scripts)
198212
{
199-
// this function will build a single .execute script from all the small scripts provided.
200-
// Execute script is defined here: https://learn.microsoft.com/en-us/kusto/management/execute-database-script?view=azure-data-explorer
201213
if (scripts.Count == 0)
202214
{
203215
return [];
@@ -215,8 +227,7 @@ internal virtual async Task<List<ScriptExecuteCommandResult>> ExecutePendingSync
215227
logger.LogDebug($"Script content:\n{script}");
216228

217229
var result = await client.AdminClient.ExecuteControlCommandAsync(databaseName, script);
218-
var resultsList = result.As<ScriptExecuteCommandResult>();
219-
return resultsList;
230+
return result.As<ScriptExecuteCommandResult>();
220231
}
221232
}
222233
}

0 commit comments

Comments
 (0)