Skip to content

Commit

Permalink
VCI-779: Refactor grabmigrator
Browse files Browse the repository at this point in the history
  • Loading branch information
krankenbro committed Jan 24, 2024
1 parent 0a56cd5 commit 6dedb75
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 116 deletions.
80 changes: 47 additions & 33 deletions src/VirtoCommerce.Build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -779,11 +779,9 @@ await PublishRelease(GitHubUser, GitRepositoryName, GitHubToken, tag, descriptio
}
catch (AggregateException ex)
{
foreach (var innerException in ex.Flatten().InnerExceptions)
foreach (var innerException in ex.Flatten().InnerExceptions.OfType<ApiValidationException>())
{
if (innerException is ApiValidationException exception)
{
var responseString = exception?.HttpResponse?.Body.ToString() ?? string.Empty;
var responseString = innerException.HttpResponse?.Body.ToString() ?? string.Empty;
var responseDocument = JsonDocument.Parse(responseString);
var alreadyExistsError = false;

Expand All @@ -804,14 +802,9 @@ await PublishRelease(GitHubUser, GitRepositoryName, GitHubToken, tag, descriptio
}

Log.Error($"Api Validation Error: {responseString}");
}
else
{
Log.Error(innerException, "Error");
}
}

Assert.Fail("Publish Release Failed");
Assert.Fail("Publish Release Failed", ex);
}
});

Expand All @@ -823,30 +816,30 @@ await PublishRelease(GitHubUser, GitRepositoryName, GitHubToken, tag, descriptio

public static int Main(string[] args)
{
if (!MSBuildLocator.IsRegistered)
{
MSBuildLocator.RegisterDefaults();
}
RegisterMSBuildLocator();

Environment.SetEnvironmentVariable("NUKE_TELEMETRY_OPTOUT", "1");
if (args.ElementAtOrDefault(0)?.ToLowerInvariant() == "help" || args.Length == 0)
{
if (args.Length >= 2)
{
var help = HelpProvider.HelpProvider.GetHelpForTarget(args[1]);
Console.WriteLine(help);
}
else if (args.Length <= 1)
{
var targets = HelpProvider.HelpProvider.GetTargets();
var stringBuilder = new StringBuilder("There is a help for targets:" + Environment.NewLine);
targets.ForEach(target => stringBuilder = stringBuilder.Append("- ").AppendLine(target));
Console.WriteLine(stringBuilder.ToString());
}

Environment.Exit(0);
CheckHelpRequested(args);

CreateNukeDirectory();

ClearTempOnExit();

var exitCode = Execute<Build>(x => x.Compile);
return _exitCode ?? exitCode;
}

private static void ClearTempOnExit()
{
if (ClearTempBeforeExit)
{
FileSystemTasks.DeleteDirectory(TemporaryDirectory);
}
}

private static void CreateNukeDirectory()
{
var currentDirectory = Directory.GetCurrentDirectory();

var nukeFiles = Directory.GetFiles(currentDirectory, ".nuke");
Expand All @@ -872,14 +865,35 @@ public static int Main(string[] args)
var nukeFile = nukeFiles[0];
ConvertDotNukeFile(nukeFile);
}
}

if (ClearTempBeforeExit)
private static void CheckHelpRequested(string[] args)
{
if (args.ElementAtOrDefault(0)?.ToLowerInvariant() == "help" || args.Length == 0)
{
FileSystemTasks.DeleteDirectory(TemporaryDirectory);
if (args.Length >= 2)
{
var help = HelpProvider.HelpProvider.GetHelpForTarget(args[1]);
Console.WriteLine(help);
}
else if (args.Length <= 1)
{
var targets = HelpProvider.HelpProvider.GetTargets();
var stringBuilder = new StringBuilder("There is a help for targets:" + Environment.NewLine);
targets.ForEach(target => stringBuilder = stringBuilder.Append("- ").AppendLine(target));
Console.WriteLine(stringBuilder.ToString());
}

Environment.Exit(0);
}
}

var exitCode = Execute<Build>(x => x.Compile);
return _exitCode ?? exitCode;
private static void RegisterMSBuildLocator()
{
if (!MSBuildLocator.IsRegistered)
{
MSBuildLocator.RegisterDefaults();
}
}

private static void ConvertDotNukeFile(string path)
Expand Down
178 changes: 95 additions & 83 deletions src/VirtoCommerce.Build/GrabMigrator/GrabMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,93 +36,12 @@ public void Do(string configFilePath)

if (config?.Grab == true)
{
OutBox("Grab mode");

Out(@"Refresh connection strings references...");
config.ConnectionStringsRefs = new Dictionary<string, List<string>>();

foreach (var migrationDirectory in config.MigrationDirectories)
{
Out($@"Looking in {migrationDirectory}...");
GrabConnectionStringsRefsFromModules(config.ConnectionStringsRefs, migrationDirectory);
}

File.WriteAllText(configFilePath, JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true }));

Out(@"Looking for migrations in migration directories recursively...");
sqlStatements = GrabSqlStatements(config);
sqlStatements = EnableGrabMode(configFilePath, config, sqlStatements);
}

if (config?.Apply == true)
{
OutBox("Apply mode");

sqlStatements ??= ReadSavedStatements(config.StatementsDirectory);

if (sqlStatements != null)
{
Out(@"Read platform config file...");

var connectionStrings = GrabConnectionStrings(config.PlatformConfigFile);

foreach (var module in config.ApplyingOrder)
{
OutBox($@"Applying scripts for module: {module}...");

if (!sqlStatements.ContainsKey(module))
{
Out($@"Warning! There is no SQL expressions for module: {module}");
continue;
}

var connectionString = string.Empty;

if (config.ConnectionStringsRefs.ContainsKey(module))
{
foreach (var moduleConnStringKey in config.ConnectionStringsRefs[module])
{
connectionString = connectionStrings.ContainsKey(moduleConnStringKey) ? connectionStrings[moduleConnStringKey] : string.Empty;

if (!string.IsNullOrEmpty(connectionString))
{
break;
}
}
}

// Fallback connection string key is always "VirtoCommerce"
connectionString = connectionString.EmptyToNull() ?? connectionStrings["VirtoCommerce"];

using (var connection = (IDbConnection)new SqlConnection(connectionString))
{
// One connection and transaction per each module
connection.Open();
var transaction = connection.BeginTransaction();

try
{
foreach (var commandText in sqlStatements[module])
{
Out($@"Run SQL statement:{Environment.NewLine}{commandText}");
var command = connection.CreateCommand();
command.Transaction = transaction;
command.CommandTimeout = config.CommandTimeout;
command.CommandText = commandText;
command.ExecuteNonQuery();
}

transaction.Commit();
Out($@"Successfully applied for module: {module}!");
}
catch
{
transaction.Rollback();
Out($@"Statement not executed. Transaction for module {module} rolled back.");
throw;
}
}
}
}
EnableApplyMode(config, sqlStatements);
}

OutBox(@"Complete!");
Expand All @@ -145,6 +64,99 @@ public void Do(string configFilePath)
}
}

private void EnableApplyMode(Config config, Dictionary<string, List<string>> sqlStatements)
{
OutBox("Apply mode");

sqlStatements ??= ReadSavedStatements(config.StatementsDirectory);

if (sqlStatements != null)
{
Out(@"Read platform config file...");

var connectionStrings = GrabConnectionStrings(config.PlatformConfigFile);

foreach (var module in config.ApplyingOrder)
{
OutBox($@"Applying scripts for module: {module}...");

if (!sqlStatements.ContainsKey(module))
{
Out($@"Warning! There is no SQL expressions for module: {module}");
continue;
}

var connectionString = string.Empty;

if (config.ConnectionStringsRefs.ContainsKey(module))
{
foreach (var moduleConnStringKey in config.ConnectionStringsRefs[module])
{
connectionString = connectionStrings.ContainsKey(moduleConnStringKey) ? connectionStrings[moduleConnStringKey] : string.Empty;

if (!string.IsNullOrEmpty(connectionString))
{
break;
}
}
}

// Fallback connection string key is always "VirtoCommerce"
connectionString = connectionString.EmptyToNull() ?? connectionStrings["VirtoCommerce"];

using (var connection = (IDbConnection)new SqlConnection(connectionString))
{
// One connection and transaction per each module
connection.Open();
var transaction = connection.BeginTransaction();

try
{
foreach (var commandText in sqlStatements[module])
{
Out($@"Run SQL statement:{Environment.NewLine}{commandText}");
var command = connection.CreateCommand();
command.Transaction = transaction;
command.CommandTimeout = config.CommandTimeout;
command.CommandText = commandText;
command.ExecuteNonQuery();
}

transaction.Commit();
Out($@"Successfully applied for module: {module}!");
}
catch
{
transaction.Rollback();
Out($@"Statement not executed. Transaction for module {module} rolled back.");
throw;
}
}
}
}
}

private Dictionary<string, List<string>> EnableGrabMode(string configFilePath, Config config, Dictionary<string, List<string>> sqlStatements)
{
sqlStatements ??= new Dictionary<string, List<string>>();
OutBox("Grab mode");

Out(@"Refresh connection strings references...");
config.ConnectionStringsRefs = new Dictionary<string, List<string>>();

foreach (var migrationDirectory in config.MigrationDirectories)
{
Out($@"Looking in {migrationDirectory}...");
GrabConnectionStringsRefsFromModules(config.ConnectionStringsRefs, migrationDirectory);
}

File.WriteAllText(configFilePath, JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true }));

Out(@"Looking for migrations in migration directories recursively...");
sqlStatements = GrabSqlStatements(config);
return sqlStatements;
}

private Dictionary<string, string> GrabConnectionStrings(string platformConfigFile)
{
var result = new Dictionary<string, string>();
Expand Down

0 comments on commit 6dedb75

Please sign in to comment.