1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-28 13:15:12 +01:00
bitwarden-server/util/MsSqlMigratorUtility/Program.cs

75 lines
3.0 KiB
C#
Raw Normal View History

using Bit.Migrator;
[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 CommandDotNet;
using Microsoft.Extensions.Logging;
internal class Program
{
[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
private static IDictionary<string, string> Parameters { get; set; }
private static int Main(string[] args)
{
[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
return new AppRunner<Program>().Run(args);
}
[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
[DefaultCommand]
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);
private static void WriteUsageToConsole()
{
Console.WriteLine("Usage: MsSqlMigratorUtility <database-connection-string>");
Console.WriteLine("Usage: MsSqlMigratorUtility <database-connection-string> -v|--verbose (for verbose output of migrator logs)");
[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
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)");
}
[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
private static bool MigrateDatabase(string databaseConnectionString, bool verbose = false, bool repeatable = false, string folderName = "")
{
var logger = CreateLogger(verbose);
[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
logger.LogInformation($"Migrating database with repeatable: {repeatable} and folderName: {folderName}.");
var migrator = new DbMigrator(databaseConnectionString, logger);
[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 success = false;
if (!string.IsNullOrWhiteSpace(folderName))
{
success = migrator.MigrateMsSqlDatabaseWithRetries(verbose, repeatable, folderName);
}
else
{
success = migrator.MigrateMsSqlDatabaseWithRetries(verbose, 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;
}
}