2023-03-07 14:10:34 +01:00
|
|
|
|
using Bit.Migrator;
|
2023-09-28 16:29:52 +02:00
|
|
|
|
using CommandDotNet;
|
2023-03-07 14:10:34 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
internal class Program
|
|
|
|
|
{
|
2023-09-28 16:29:52 +02:00
|
|
|
|
private static IDictionary<string, string> Parameters { get; set; }
|
|
|
|
|
|
2023-03-07 14:10:34 +01:00
|
|
|
|
private static int Main(string[] args)
|
|
|
|
|
{
|
2023-09-28 16:29:52 +02:00
|
|
|
|
return new AppRunner<Program>().Run(args);
|
2023-03-07 14:10:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
2023-03-07 14:10:34 +01:00
|
|
|
|
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)");
|
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)");
|
2023-03-07 14:10:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-28 16:29:52 +02:00
|
|
|
|
private static bool MigrateDatabase(string databaseConnectionString, bool verbose = false, bool repeatable = false, string folderName = "")
|
2023-03-07 14:10:34 +01:00
|
|
|
|
{
|
|
|
|
|
var logger = CreateLogger(verbose);
|
|
|
|
|
|
2023-09-28 16:29:52 +02:00
|
|
|
|
logger.LogInformation($"Migrating database with repeatable: {repeatable} and folderName: {folderName}.");
|
|
|
|
|
|
2023-03-07 14:10:34 +01:00
|
|
|
|
var migrator = new DbMigrator(databaseConnectionString, logger);
|
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);
|
|
|
|
|
}
|
2023-03-07 14:10:34 +01:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|