mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 07:36:14 -05:00
Add variable for production migration transaction level (#4702)
* 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 <mbishop@bitwarden.com>
This commit is contained in:
@ -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,
|
||||
@ -30,6 +32,7 @@ public class DbMigrator
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var attempt = 1;
|
||||
|
||||
while (attempt < 10)
|
||||
{
|
||||
try
|
||||
@ -69,6 +72,7 @@ public class DbMigrator
|
||||
using (var connection = new SqlConnection(masterConnectionString))
|
||||
{
|
||||
var databaseName = new SqlConnectionStringBuilder(_connectionString).InitialCatalog;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(databaseName))
|
||||
{
|
||||
databaseName = "vault";
|
||||
@ -105,10 +109,10 @@ public class DbMigrator
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
@ -121,8 +125,17 @@ public class DbMigrator
|
||||
.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)
|
||||
{
|
||||
@ -144,6 +157,7 @@ public class DbMigrator
|
||||
{
|
||||
var scriptsToExec = upgrader.GetScriptsToExecute();
|
||||
var stringBuilder = new StringBuilder("Scripts that will be applied:");
|
||||
|
||||
foreach (var script in scriptsToExec)
|
||||
{
|
||||
stringBuilder.AppendLine(script.Name);
|
||||
|
Reference in New Issue
Block a user