1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-24 12:35:25 +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.

108 lines
3.8 KiB
C#
Raw Normal View History

using System.Data;
2019-03-25 14:38:04 +01:00
using System.Reflection;
2019-03-25 20:20:54 +01:00
using Bit.Core;
2019-03-25 14:38:04 +01:00
using DbUp;
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 string _masterConnectionString;
2022-08-29 22:06:55 +02:00
2019-03-25 18:21:05 +01:00
public DbMigrator(string connectionString, ILogger<DbMigrator> logger)
2019-03-25 14:38:04 +01:00
{
2019-03-25 18:21:05 +01:00
_connectionString = connectionString;
_logger = logger;
_masterConnectionString = new SqlConnectionStringBuilder(connectionString)
2022-08-29 22:06:55 +02:00
{
2019-03-25 18:21:05 +01:00
InitialCatalog = "master"
}.ConnectionString;
2022-08-29 22:06:55 +02:00
}
2019-03-25 18:21:05 +01:00
public bool MigrateMsSqlDatabase(bool enableLogging = true,
CancellationToken cancellationToken = default(CancellationToken))
2022-08-29 22:06:55 +02:00
{
2019-03-25 18:21:05 +01:00
if (enableLogging && _logger != null)
2019-03-25 14:38:04 +01:00
{
2019-03-25 18:21:05 +01:00
_logger.LogInformation(Constants.BypassFiltersEventId, "Migrating database.");
}
2019-03-25 14:38:04 +01:00
2019-03-25 18:21:05 +01:00
using (var connection = new SqlConnection(_masterConnectionString))
{
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();
var builder = DeployChanges.To
.SqlDatabase(_connectionString)
.JournalToSqlTable("dbo", "Migration")
.WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(),
s => s.Contains($".DbScripts.") && !s.Contains(".Archive."))
.WithTransaction()
.WithExecutionTimeout(new TimeSpan(0, 5, 0));
2022-08-29 22:06:55 +02:00
if (enableLogging)
2022-08-29 22:06:55 +02:00
{
if (_logger != null)
2019-03-25 18:21:05 +01:00
{
builder.LogTo(new DbUpLogger(_logger));
2019-03-25 14:38:04 +01:00
}
2019-03-25 18:21:05 +01:00
else
2019-03-25 14:38:04 +01:00
{
2019-03-25 20:20:54 +01:00
builder.LogToConsole();
2019-03-25 18:21:05 +01:00
}
2022-08-29 22:06:55 +02:00
}
var upgrader = builder.Build();
2019-03-25 18:21:05 +01:00
var result = upgrader.PerformUpgrade();
2019-03-25 18:21:05 +01:00
if (enableLogging && _logger != null)
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
}
}