Skip to content

Commit

Permalink
VCI-779: Refactor GrabMigrator.EnableApplyMode
Browse files Browse the repository at this point in the history
  • Loading branch information
krankenbro committed Jan 24, 2024
1 parent 6dedb75 commit 7d0b2bd
Showing 1 changed file with 44 additions and 47 deletions.
91 changes: 44 additions & 47 deletions src/VirtoCommerce.Build/GrabMigrator/GrabMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,67 +70,64 @@ private void EnableApplyMode(Config config, Dictionary<string, List<string>> sql

sqlStatements ??= ReadSavedStatements(config.StatementsDirectory);

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

var connectionStrings = GrabConnectionStrings(config.PlatformConfigFile);
var connectionStrings = GrabConnectionStrings(config.PlatformConfigFile);

foreach (var module in config.ApplyingOrder)
{
OutBox($@"Applying scripts for module: {module}...");
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;
}
if (!sqlStatements.ContainsKey(module))
{
Out($@"Warning! There is no SQL expressions for module: {module}");
continue;
}

var connectionString = string.Empty;
var connectionString = string.Empty;

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

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

// Fallback connection string key is always "VirtoCommerce"
connectionString = connectionString.EmptyToNull() ?? connectionStrings["VirtoCommerce"];
// 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();
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
try
{
foreach (var commandText in sqlStatements[module])
{
transaction.Rollback();
Out($@"Statement not executed. Transaction for module {module} rolled back.");
throw;
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;
}
}
}
Expand Down

0 comments on commit 7d0b2bd

Please sign in to comment.