mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 07:36:14 -05:00
[DEVOPS-1215] Build migrator CLI project (#2747)
* Add migrator cli * Ran format * Acc build workflow * Change paths in push and pr triggers * Add build migrator cli to build workflow * Remove build migrator cli workflow * Add different levels of logs for verbose * Rename migratorCLI to MsSqlMigratorUtility * Add MsSqlMigratorUtility to solution file * Remove the clean command * Fix name and path in build workflow * Add retry logic to DbMigrator instead of invocation * Add migrator with retry mechanism as a new method * Log the migration start log to migrate database method * Fix name in build * Fix cli leftovers * Fix exception var name * String interpolation * Remove redundant check for number * Remove CommandDotNet * dotnet format * Remove CommandDotNet from packages lock * Remove all cli * Trying to remove usings to see if this fixes linter * Add usings back again - uild is failing * Remove implicit usings * Trying to fix linter issues * Trying to fix linter
This commit is contained in:
15
util/MsSqlMigratorUtility/MsSqlMigratorUtility.csproj
Normal file
15
util/MsSqlMigratorUtility/MsSqlMigratorUtility.csproj
Normal file
@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Migrator\Migrator.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
78
util/MsSqlMigratorUtility/Program.cs
Normal file
78
util/MsSqlMigratorUtility/Program.cs
Normal file
@ -0,0 +1,78 @@
|
||||
using Bit.Migrator;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
private static int Main(string[] args)
|
||||
{
|
||||
if (args.Length == 0)
|
||||
{
|
||||
Console.WriteLine("Please enter a database connection string argument.");
|
||||
WriteUsageToConsole();
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (args.Length == 1 && (args[0] == "--verbose" || args[0] == "-v"))
|
||||
{
|
||||
Console.WriteLine($"Please enter a database connection string argument before {args[0]} option.");
|
||||
WriteUsageToConsole();
|
||||
return 1;
|
||||
}
|
||||
|
||||
var databaseConnectionString = args[0];
|
||||
|
||||
var verbose = false;
|
||||
|
||||
if (args.Length == 2 && (args[1] == "--verbose" || args[1] == "-v"))
|
||||
{
|
||||
verbose = true;
|
||||
}
|
||||
|
||||
var success = MigrateDatabase(databaseConnectionString, verbose);
|
||||
|
||||
if (!success)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
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)");
|
||||
}
|
||||
|
||||
private static bool MigrateDatabase(string databaseConnectionString, bool verbose = false, int attempt = 1)
|
||||
{
|
||||
var logger = CreateLogger(verbose);
|
||||
|
||||
var migrator = new DbMigrator(databaseConnectionString, logger);
|
||||
var success = migrator.MigrateMsSqlDatabaseWithRetries(verbose);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
2723
util/MsSqlMigratorUtility/packages.lock.json
Normal file
2723
util/MsSqlMigratorUtility/packages.lock.json
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user