1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00
bitwarden-server/util/Migrator/DbMigrator.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

203 lines
6.5 KiB
C#
Raw Normal View History

using System.Data;
2019-03-25 14:38:04 +01:00
using System.Reflection;
using System.Text;
2019-03-25 20:20:54 +01:00
using Bit.Core;
2019-03-25 14:38:04 +01:00
using DbUp;
[DEVOPS-1519] Add transition mode to mssql migrator utility (#3259) * Add RerunableSqlTableJournal * Add extension to use rerunable sql table journal * Use rerunable sql journal * format * Enable logging * FIx * Disable logging * Rename to SqlTableJournalExtensions * Move RerunableSqlTableJournal to Extension class * Fix usings * Add rerunable schema * Format * Fix typo * Enable logging in db migrator * add rerunable column in dbo migrations table migration * Trying * Fix journal table name * Trying to migrate first * After migration * Testing * Add update from rerunable to not rerunable script * Change name * Add rerunable option and script folder name * Add rerunable options and folder * Fix * Add transition (aka rerunable) migrations to Setup * Parse parameters on migrator utility * Fix sql scripts * Remove CreateSchemaTableSql as it'll be migrated using migration * Embed dbScripts_data_migration folder * Remove testing sql script * Add optins parsing nuget for msSqlMigratorUtility * Fix sql journal * Ran dotnet format * Comment out index * ▫️Revert "Comment out index" This reverts commit df15fa91e05d5b195e130c36e5ae51fc508b2506. * Disable logging * Add newline * Rename rerunable to repeatable * remove repeatable journal * Remove migration adding the repeatable column in dbo.Migrations table * Add using * Enable log for testing * Disable logging in the setup * Remove unused method * Add migrator constants * Use constants in yet another place * Fix * Add constant * Fix * Fix
2023-09-28 16:29:52 +02:00
using DbUp.Helpers;
using Microsoft.Data.SqlClient;
2019-03-25 18:21:05 +01:00
using Microsoft.Extensions.Logging;
2019-03-25 14:38:04 +01:00
namespace Bit.Migrator;
2022-08-29 22:06:55 +02:00
2019-03-25 14:38:04 +01:00
public class DbMigrator
{
2019-03-25 18:21:05 +01:00
private readonly string _connectionString;
private readonly ILogger<DbMigrator> _logger;
private readonly bool _skipDatabasePreparation;
private readonly bool _noTransactionMigration;
2022-08-29 22:06:55 +02:00
public DbMigrator(string connectionString, ILogger<DbMigrator> logger = null,
bool skipDatabasePreparation = false, bool noTransactionMigration = false)
2019-03-25 14:38:04 +01:00
{
2019-03-25 18:21:05 +01:00
_connectionString = connectionString;
_logger = logger ?? CreateLogger();
_skipDatabasePreparation = skipDatabasePreparation;
_noTransactionMigration = noTransactionMigration;
2022-08-29 22:06:55 +02:00
}
2019-03-25 18:21:05 +01:00
public bool MigrateMsSqlDatabaseWithRetries(bool enableLogging = true,
[DEVOPS-1519] Add transition mode to mssql migrator utility (#3259) * Add RerunableSqlTableJournal * Add extension to use rerunable sql table journal * Use rerunable sql journal * format * Enable logging * FIx * Disable logging * Rename to SqlTableJournalExtensions * Move RerunableSqlTableJournal to Extension class * Fix usings * Add rerunable schema * Format * Fix typo * Enable logging in db migrator * add rerunable column in dbo migrations table migration * Trying * Fix journal table name * Trying to migrate first * After migration * Testing * Add update from rerunable to not rerunable script * Change name * Add rerunable option and script folder name * Add rerunable options and folder * Fix * Add transition (aka rerunable) migrations to Setup * Parse parameters on migrator utility * Fix sql scripts * Remove CreateSchemaTableSql as it'll be migrated using migration * Embed dbScripts_data_migration folder * Remove testing sql script * Add optins parsing nuget for msSqlMigratorUtility * Fix sql journal * Ran dotnet format * Comment out index * ▫️Revert "Comment out index" This reverts commit df15fa91e05d5b195e130c36e5ae51fc508b2506. * Disable logging * Add newline * Rename rerunable to repeatable * remove repeatable journal * Remove migration adding the repeatable column in dbo.Migrations table * Add using * Enable log for testing * Disable logging in the setup * Remove unused method * Add migrator constants * Use constants in yet another place * Fix * Add constant * Fix * Fix
2023-09-28 16:29:52 +02:00
bool repeatable = false,
string folderName = MigratorConstants.DefaultMigrationsFolderName,
bool dryRun = false,
CancellationToken cancellationToken = default)
2022-08-29 22:06:55 +02:00
{
var attempt = 1;
while (attempt < 10)
{
try
{
if (!_skipDatabasePreparation)
{
PrepareDatabase(cancellationToken);
}
var success = MigrateDatabase(enableLogging, repeatable, folderName, dryRun, cancellationToken);
return success;
}
catch (SqlException ex)
{
if (ex.Message.Contains("Server is in script upgrade mode."))
{
attempt++;
_logger.LogInformation($"Database is in script upgrade mode, trying again (attempt #{attempt}).");
Thread.Sleep(20000);
}
else
{
throw;
}
}
}
return false;
}
private void PrepareDatabase(CancellationToken cancellationToken = default)
{
var masterConnectionString = new SqlConnectionStringBuilder(_connectionString)
2019-03-25 14:38:04 +01:00
{
InitialCatalog = "master"
}.ConnectionString;
2019-03-25 14:38:04 +01:00
using (var connection = new SqlConnection(masterConnectionString))
2019-03-25 18:21:05 +01:00
{
var databaseName = new SqlConnectionStringBuilder(_connectionString).InitialCatalog;
if (string.IsNullOrWhiteSpace(databaseName))
2019-03-25 14:38:04 +01:00
{
2019-03-25 20:20:54 +01:00
databaseName = "vault";
2019-03-25 18:21:05 +01:00
}
2019-03-25 14:38:04 +01:00
2019-03-25 18:21:05 +01:00
var databaseNameQuoted = new SqlCommandBuilder().QuoteIdentifier(databaseName);
var command = new SqlCommand(
"IF ((SELECT COUNT(1) FROM sys.databases WHERE [name] = @DatabaseName) = 0) " +
"CREATE DATABASE " + databaseNameQuoted + ";", connection);
command.Parameters.Add("@DatabaseName", SqlDbType.VarChar).Value = databaseName;
command.Connection.Open();
command.ExecuteNonQuery();
2019-03-25 18:21:05 +01:00
command.CommandText = "IF ((SELECT DATABASEPROPERTYEX([name], 'IsAutoClose') " +
"FROM sys.databases WHERE [name] = @DatabaseName) = 1) " +
"ALTER DATABASE " + databaseNameQuoted + " SET AUTO_CLOSE OFF;";
command.ExecuteNonQuery();
2022-08-29 22:06:55 +02:00
}
2022-08-29 20:53:16 +02:00
cancellationToken.ThrowIfCancellationRequested();
using (var connection = new SqlConnection(_connectionString))
2022-08-29 22:06:55 +02:00
{
// rename old migration scripts to new namespace
var command = new SqlCommand(
"IF OBJECT_ID('Migration','U') IS NOT NULL " +
"UPDATE [dbo].[Migration] SET " +
"[ScriptName] = REPLACE([ScriptName], 'Bit.Setup.', 'Bit.Migrator.');", connection);
2019-03-25 20:59:12 +01:00
command.Connection.Open();
command.ExecuteNonQuery();
2019-03-25 18:21:05 +01:00
}
2019-03-25 18:21:05 +01:00
cancellationToken.ThrowIfCancellationRequested();
}
private bool MigrateDatabase(bool enableLogging = true,
bool repeatable = false,
string folderName = MigratorConstants.DefaultMigrationsFolderName,
bool dryRun = false,
CancellationToken cancellationToken = default)
{
if (enableLogging)
{
_logger.LogInformation(Constants.BypassFiltersEventId, "Migrating database.");
}
cancellationToken.ThrowIfCancellationRequested();
var builder = DeployChanges.To
.SqlDatabase(_connectionString)
.WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(),
[DEVOPS-1519] Add transition mode to mssql migrator utility (#3259) * Add RerunableSqlTableJournal * Add extension to use rerunable sql table journal * Use rerunable sql journal * format * Enable logging * FIx * Disable logging * Rename to SqlTableJournalExtensions * Move RerunableSqlTableJournal to Extension class * Fix usings * Add rerunable schema * Format * Fix typo * Enable logging in db migrator * add rerunable column in dbo migrations table migration * Trying * Fix journal table name * Trying to migrate first * After migration * Testing * Add update from rerunable to not rerunable script * Change name * Add rerunable option and script folder name * Add rerunable options and folder * Fix * Add transition (aka rerunable) migrations to Setup * Parse parameters on migrator utility * Fix sql scripts * Remove CreateSchemaTableSql as it'll be migrated using migration * Embed dbScripts_data_migration folder * Remove testing sql script * Add optins parsing nuget for msSqlMigratorUtility * Fix sql journal * Ran dotnet format * Comment out index * ▫️Revert "Comment out index" This reverts commit df15fa91e05d5b195e130c36e5ae51fc508b2506. * Disable logging * Add newline * Rename rerunable to repeatable * remove repeatable journal * Remove migration adding the repeatable column in dbo.Migrations table * Add using * Enable log for testing * Disable logging in the setup * Remove unused method * Add migrator constants * Use constants in yet another place * Fix * Add constant * Fix * Fix
2023-09-28 16:29:52 +02:00
s => s.Contains($".{folderName}.") && !s.Contains(".Archive."))
.WithExecutionTimeout(TimeSpan.FromMinutes(5));
if (_noTransactionMigration)
{
builder = builder.WithoutTransaction()
.WithExecutionTimeout(TimeSpan.FromMinutes(60));
}
else
{
builder = builder.WithTransaction();
}
2022-08-29 22:06:55 +02:00
[DEVOPS-1519] Add transition mode to mssql migrator utility (#3259) * Add RerunableSqlTableJournal * Add extension to use rerunable sql table journal * Use rerunable sql journal * format * Enable logging * FIx * Disable logging * Rename to SqlTableJournalExtensions * Move RerunableSqlTableJournal to Extension class * Fix usings * Add rerunable schema * Format * Fix typo * Enable logging in db migrator * add rerunable column in dbo migrations table migration * Trying * Fix journal table name * Trying to migrate first * After migration * Testing * Add update from rerunable to not rerunable script * Change name * Add rerunable option and script folder name * Add rerunable options and folder * Fix * Add transition (aka rerunable) migrations to Setup * Parse parameters on migrator utility * Fix sql scripts * Remove CreateSchemaTableSql as it'll be migrated using migration * Embed dbScripts_data_migration folder * Remove testing sql script * Add optins parsing nuget for msSqlMigratorUtility * Fix sql journal * Ran dotnet format * Comment out index * ▫️Revert "Comment out index" This reverts commit df15fa91e05d5b195e130c36e5ae51fc508b2506. * Disable logging * Add newline * Rename rerunable to repeatable * remove repeatable journal * Remove migration adding the repeatable column in dbo.Migrations table * Add using * Enable log for testing * Disable logging in the setup * Remove unused method * Add migrator constants * Use constants in yet another place * Fix * Add constant * Fix * Fix
2023-09-28 16:29:52 +02:00
if (repeatable)
{
builder.JournalTo(new NullJournal());
}
else
{
builder.JournalToSqlTable("dbo", MigratorConstants.SqlTableJournalName);
}
if (enableLogging)
2022-08-29 22:06:55 +02:00
{
builder.LogTo(new DbUpLogger(_logger));
2022-08-29 22:06:55 +02:00
}
var upgrader = builder.Build();
if (dryRun)
{
var scriptsToExec = upgrader.GetScriptsToExecute();
var stringBuilder = new StringBuilder("Scripts that will be applied:");
foreach (var script in scriptsToExec)
{
stringBuilder.AppendLine(script.Name);
}
_logger.LogInformation(Constants.BypassFiltersEventId, stringBuilder.ToString());
return true;
}
2019-03-25 18:21:05 +01:00
var result = upgrader.PerformUpgrade();
if (enableLogging)
2022-08-29 22:06:55 +02:00
{
if (result.Successful)
2019-03-25 18:21:05 +01:00
{
2019-03-25 20:20:54 +01:00
_logger.LogInformation(Constants.BypassFiltersEventId, "Migration successful.");
}
else
{
2019-03-25 20:20:54 +01:00
_logger.LogError(Constants.BypassFiltersEventId, result.Error, "Migration failed.");
2019-03-25 14:38:04 +01:00
}
}
2022-08-29 22:06:55 +02:00
2019-03-25 20:59:12 +01:00
cancellationToken.ThrowIfCancellationRequested();
2019-03-25 18:21:05 +01:00
return result.Successful;
2019-03-25 14:38:04 +01:00
}
private ILogger<DbMigrator> CreateLogger()
{
var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddConsole();
builder.AddFilter("DbMigrator.DbMigrator", LogLevel.Information);
});
return loggerFactory.CreateLogger<DbMigrator>();
}
2019-03-25 14:38:04 +01:00
}