Skip to content

Commit

Permalink
Add variable for production migration transaction level (#4702)
Browse files Browse the repository at this point in the history
* Addd variable for production migration transaction level

* Added variable for production migration transaction level with default value

* Clean up comments

* Removed uneeded directive

* Changed time format for timeout on migration

* white space formatting

* white space formatting again

* white space formatting once again

* white space formatting once again

* clean up

* CHnaged to builder.WithoutTransaction()

* Changed to optyion flag from n to nt for notransaction

* Changed to optyion flag from n to no-transaction for  without transaction

* Change desription of option

---------

Co-authored-by: Matt Bishop <[email protected]>
(cherry picked from commit b38b537)
  • Loading branch information
rkac-bw authored and vgrassia committed Oct 10, 2024
1 parent d22b5e8 commit ad8aec9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
28 changes: 21 additions & 7 deletions util/Migrator/DbMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ public class DbMigrator
private readonly string _connectionString;
private readonly ILogger<DbMigrator> _logger;
private readonly bool _skipDatabasePreparation;
private readonly bool _noTransactionMigration;

public DbMigrator(string connectionString, ILogger<DbMigrator> logger = null,
bool skipDatabasePreparation = false)
bool skipDatabasePreparation = false, bool noTransactionMigration = false)
{
_connectionString = connectionString;
_logger = logger ?? CreateLogger();
_skipDatabasePreparation = skipDatabasePreparation;
_noTransactionMigration = noTransactionMigration;
}

public bool MigrateMsSqlDatabaseWithRetries(bool enableLogging = true,
Expand All @@ -30,6 +32,7 @@ public bool MigrateMsSqlDatabaseWithRetries(bool enableLogging = true,
CancellationToken cancellationToken = default)
{
var attempt = 1;

while (attempt < 10)
{
try
Expand Down Expand Up @@ -69,6 +72,7 @@ private void PrepareDatabase(CancellationToken cancellationToken = default)
using (var connection = new SqlConnection(masterConnectionString))
{
var databaseName = new SqlConnectionStringBuilder(_connectionString).InitialCatalog;

if (string.IsNullOrWhiteSpace(databaseName))
{
databaseName = "vault";
Expand Down Expand Up @@ -105,10 +109,10 @@ private void PrepareDatabase(CancellationToken cancellationToken = default)
}

private bool MigrateDatabase(bool enableLogging = true,
bool repeatable = false,
string folderName = MigratorConstants.DefaultMigrationsFolderName,
bool dryRun = false,
CancellationToken cancellationToken = default)
bool repeatable = false,
string folderName = MigratorConstants.DefaultMigrationsFolderName,
bool dryRun = false,
CancellationToken cancellationToken = default)
{
if (enableLogging)
{
Expand All @@ -121,8 +125,17 @@ private bool MigrateDatabase(bool enableLogging = true,
.SqlDatabase(_connectionString)
.WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(),
s => s.Contains($".{folderName}.") && !s.Contains(".Archive."))
.WithTransaction()
.WithExecutionTimeout(new TimeSpan(0, 5, 0));
.WithExecutionTimeout(TimeSpan.FromMinutes(5));

if (_noTransactionMigration)
{
builder = builder.WithoutTransaction()
.WithExecutionTimeout(TimeSpan.FromMinutes(60));
}
else
{
builder = builder.WithTransaction();
}

if (repeatable)
{
Expand All @@ -144,6 +157,7 @@ private bool MigrateDatabase(bool enableLogging = true,
{
var scriptsToExec = upgrader.GetScriptsToExecute();
var stringBuilder = new StringBuilder("Scripts that will be applied:");

foreach (var script in scriptsToExec)
{
stringBuilder.AppendLine(script.Name);
Expand Down
10 changes: 6 additions & 4 deletions util/MsSqlMigratorUtility/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ public void Execute(
[Option('f', "folder", Description = "Folder name of database scripts")]
string folderName = MigratorConstants.DefaultMigrationsFolderName,
[Option('d', "dry-run", Description = "Print the scripts that will be applied without actually executing them")]
bool dryRun = false
) => MigrateDatabase(databaseConnectionString, repeatable, folderName, dryRun);
bool dryRun = false,
[Option("no-transaction", Description = "Run without adding transaction per script or all scripts")]
bool noTransactionMigration = false
) => MigrateDatabase(databaseConnectionString, repeatable, folderName, dryRun, noTransactionMigration);

private static bool MigrateDatabase(string databaseConnectionString,
bool repeatable = false, string folderName = "", bool dryRun = false)
bool repeatable = false, string folderName = "", bool dryRun = false, bool noTransactionMigration = false)
{
var migrator = new DbMigrator(databaseConnectionString);
var migrator = new DbMigrator(databaseConnectionString, noTransactionMigration: noTransactionMigration);
bool success;
if (!string.IsNullOrWhiteSpace(folderName))
{
Expand Down

0 comments on commit ad8aec9

Please sign in to comment.