1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 07:36:14 -05:00

[PM-6909] Centralize database migration logic (#3910)

* Centralize database migration logic

* Clean up unused usings

* Prizatize

* Remove verbose flag from Docker invocation

* Allow argument passthrough still

Co-authored-by: Michał Chęciński <mchecinski@bitwarden.com>

* Allow DI logger

---------

Co-authored-by: Michał Chęciński <mchecinski@bitwarden.com>
This commit is contained in:
Matt Bishop
2024-03-22 10:54:13 -04:00
committed by GitHub
parent 1c2acbec3a
commit 743465273c
5 changed files with 71 additions and 176 deletions

View File

@ -5,4 +5,4 @@ LABEL com.bitwarden.product="bitwarden"
WORKDIR /app
COPY obj/build-output/publish .
ENTRYPOINT ["sh", "-c", "dotnet /app/MsSqlMigratorUtility.dll \"${MSSQL_CONN_STRING}\" -v ${@}", "--" ]
ENTRYPOINT ["sh", "-c", "dotnet /app/MsSqlMigratorUtility.dll \"${MSSQL_CONN_STRING}\" ${@}", "--" ]

View File

@ -1,11 +1,8 @@
using Bit.Migrator;
using CommandDotNet;
using Microsoft.Extensions.Logging;
internal class Program
{
private static IDictionary<string, string> Parameters { get; set; }
private static int Main(string[] args)
{
return new AppRunner<Program>().Run(args);
@ -15,60 +12,26 @@ internal class Program
public void Execute(
[Operand(Description = "Database connection string")]
string databaseConnectionString,
[Option('v', "verbose", Description = "Enable verbose output of migrator logs")]
bool verbose = false,
[Option('r', "repeatable", Description = "Mark scripts as repeatable")]
bool repeatable = false,
[Option('f', "folder", Description = "Folder name of database scripts")]
string folderName = MigratorConstants.DefaultMigrationsFolderName) => MigrateDatabase(databaseConnectionString, verbose, repeatable, folderName);
string folderName = MigratorConstants.DefaultMigrationsFolderName)
=> MigrateDatabase(databaseConnectionString, repeatable, folderName);
private static void WriteUsageToConsole()
private static bool MigrateDatabase(string databaseConnectionString,
bool repeatable = false, string folderName = "")
{
Console.WriteLine("Usage: MsSqlMigratorUtility <database-connection-string>");
Console.WriteLine("Usage: MsSqlMigratorUtility <database-connection-string> -v|--verbose (for verbose output of migrator logs)");
Console.WriteLine("Usage: MsSqlMigratorUtility <database-connection-string> -r|--repeatable (for marking scripts as repeatable) -f|--folder <folder-name-in-migrator-project> (for specifying folder name of scripts)");
Console.WriteLine("Usage: MsSqlMigratorUtility <database-connection-string> -v|--verbose (for verbose output of migrator logs) -r|--repeatable (for marking scripts as repeatable) -f|--folder <folder-name-in-migrator-project> (for specifying folder name of scripts)");
}
private static bool MigrateDatabase(string databaseConnectionString, bool verbose = false, bool repeatable = false, string folderName = "")
{
var logger = CreateLogger(verbose);
logger.LogInformation($"Migrating database with repeatable: {repeatable} and folderName: {folderName}.");
var migrator = new DbMigrator(databaseConnectionString, logger);
bool success = false;
var migrator = new DbMigrator(databaseConnectionString);
bool success;
if (!string.IsNullOrWhiteSpace(folderName))
{
success = migrator.MigrateMsSqlDatabaseWithRetries(verbose, repeatable, folderName);
success = migrator.MigrateMsSqlDatabaseWithRetries(true, repeatable, folderName);
}
else
{
success = migrator.MigrateMsSqlDatabaseWithRetries(verbose, repeatable);
success = migrator.MigrateMsSqlDatabaseWithRetries(true, repeatable);
}
return success;
}
private static ILogger<DbMigrator> CreateLogger(bool verbose)
{
var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddConsole();
if (verbose)
{
builder.AddFilter("DbMigrator.DbMigrator", LogLevel.Debug);
}
else
{
builder.AddFilter("DbMigrator.DbMigrator", LogLevel.Information);
}
});
var logger = loggerFactory.CreateLogger<DbMigrator>();
return logger;
}
}