1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 23:52:50 -05:00

Revert "[DEVOPS-1215] Build migrator CLI project (#2747)" (#2769)

This reverts commit 9cbf254fef.
This commit is contained in:
Michał Chęciński
2023-03-06 18:02:19 +01:00
committed by GitHub
parent 9cbf254fef
commit f8cbd4ef7d
7 changed files with 34 additions and 2912 deletions

View File

@ -23,40 +23,10 @@ public class DbMigrator
}.ConnectionString;
}
public bool MigrateMsSqlDatabaseWithRetries(bool enableLogging = true,
public bool MigrateMsSqlDatabase(bool enableLogging = true,
CancellationToken cancellationToken = default(CancellationToken))
{
var attempt = 1;
while (attempt < 10)
{
try
{
var success = MigrateDatabase(enableLogging, cancellationToken);
return success;
}
catch (SqlException ex)
{
if (ex.Message.Contains("Server is in script upgrade mode"))
{
attempt++;
_logger.LogInformation("Database is in script upgrade mode. " +
$"Trying again (attempt #{attempt})...");
Thread.Sleep(20000);
}
else
{
throw;
}
}
}
return false;
}
public bool MigrateDatabase(bool enableLogging = true,
CancellationToken cancellationToken = default(CancellationToken))
{
if (_logger != null)
if (enableLogging && _logger != null)
{
_logger.LogInformation(Constants.BypassFiltersEventId, "Migrating database.");
}
@ -119,7 +89,7 @@ public class DbMigrator
var upgrader = builder.Build();
var result = upgrader.PerformUpgrade();
if (_logger != null)
if (enableLogging && _logger != null)
{
if (result.Successful)
{

View File

@ -1,15 +0,0 @@
<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>

View File

@ -1,78 +0,0 @@
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;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,7 @@
using System.Globalization;
using System.Net.Http.Json;
using Bit.Migrator;
using Microsoft.Data.SqlClient;
namespace Bit.Setup;
@ -186,10 +187,35 @@ public class Program
private static void MigrateDatabase(int attempt = 1)
{
var vaultConnectionString = Helpers.GetValueFromEnvFile("global",
"globalSettings__sqlServer__connectionString");
var migrator = new DbMigrator(vaultConnectionString, null);
migrator.MigrateMsSqlDatabaseWithRetries(false);
try
{
Helpers.WriteLine(_context, "Migrating database.");
var vaultConnectionString = Helpers.GetValueFromEnvFile("global",
"globalSettings__sqlServer__connectionString");
var migrator = new DbMigrator(vaultConnectionString, null);
var success = migrator.MigrateMsSqlDatabase(false);
if (success)
{
Helpers.WriteLine(_context, "Migration successful.");
}
else
{
Helpers.WriteLine(_context, "Migration failed.");
}
}
catch (SqlException e)
{
if (e.Message.Contains("Server is in script upgrade mode") && attempt < 10)
{
var nextAttempt = attempt + 1;
Helpers.WriteLine(_context, "Database is in script upgrade mode. " +
"Trying again (attempt #{0})...", nextAttempt);
System.Threading.Thread.Sleep(20000);
MigrateDatabase(nextAttempt);
return;
}
throw;
}
}
private static bool ValidateInstallation()