mirror of
https://github.com/bitwarden/server.git
synced 2024-11-21 12:05:42 +01: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:
parent
27adaf59b4
commit
9cbf254fef
56
.github/workflows/build.yml
vendored
56
.github/workflows/build.yml
vendored
@ -467,6 +467,58 @@ jobs:
|
|||||||
path: swagger.json
|
path: swagger.json
|
||||||
if-no-files-found: error
|
if-no-files-found: error
|
||||||
|
|
||||||
|
build-mssqlmigratorutility:
|
||||||
|
name: Build MsSqlMigratorUtility
|
||||||
|
runs-on: ubuntu-22.04
|
||||||
|
needs:
|
||||||
|
- lint
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
shell: bash
|
||||||
|
working-directory: "util/MsSqlMigratorUtility"
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
target:
|
||||||
|
- osx-x64
|
||||||
|
- linux-x64
|
||||||
|
- win-x64
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout repo
|
||||||
|
uses: actions/checkout@a12a3943b4bdde767164f792f33f40b04645d846
|
||||||
|
|
||||||
|
- name: Print environment
|
||||||
|
run: |
|
||||||
|
whoami
|
||||||
|
dotnet --info
|
||||||
|
echo "GitHub ref: $GITHUB_REF"
|
||||||
|
echo "GitHub event: $GITHUB_EVENT"
|
||||||
|
|
||||||
|
- name: Restore project
|
||||||
|
run: |
|
||||||
|
echo "Restore"
|
||||||
|
dotnet restore
|
||||||
|
|
||||||
|
- name: Publish project
|
||||||
|
run: dotnet publish -c "Release" -o obj/build-output/publish -r ${{ matrix.target }} --self-contained true
|
||||||
|
|
||||||
|
- name: Upload project artifact Windows
|
||||||
|
if: ${{ contains(matrix.target, 'win') == true }}
|
||||||
|
uses: actions/upload-artifact@6673cd052c4cd6fcf4b4e6e60ea986c889389535
|
||||||
|
with:
|
||||||
|
name: MsSqlMigratorUtility-${{ matrix.target }}
|
||||||
|
path: util/MsSqlMigratorUtility/obj/build-output/publish/MsSqlMigratorUtility.exe
|
||||||
|
if-no-files-found: error
|
||||||
|
|
||||||
|
- name: Upload project artifact
|
||||||
|
if: ${{ contains(matrix.target, 'win') == false }}
|
||||||
|
uses: actions/upload-artifact@6673cd052c4cd6fcf4b4e6e60ea986c889389535
|
||||||
|
with:
|
||||||
|
name: MsSqlMigratorUtility-${{ matrix.target }}
|
||||||
|
path: util/MsSqlMigratorUtility/obj/build-output/publish/MsSqlMigratorUtility
|
||||||
|
if-no-files-found: error
|
||||||
|
|
||||||
check-failures:
|
check-failures:
|
||||||
name: Check for failures
|
name: Check for failures
|
||||||
if: always()
|
if: always()
|
||||||
@ -478,6 +530,7 @@ jobs:
|
|||||||
- build-artifacts
|
- build-artifacts
|
||||||
- build-docker
|
- build-docker
|
||||||
- upload
|
- upload
|
||||||
|
- build-mssqlmigratorutility
|
||||||
steps:
|
steps:
|
||||||
- name: Check if any job failed
|
- name: Check if any job failed
|
||||||
if: |
|
if: |
|
||||||
@ -491,6 +544,7 @@ jobs:
|
|||||||
BUILD_ARTIFACTS_STATUS: ${{ needs.build-artifacts.result }}
|
BUILD_ARTIFACTS_STATUS: ${{ needs.build-artifacts.result }}
|
||||||
BUILD_DOCKER_STATUS: ${{ needs.build-docker.result }}
|
BUILD_DOCKER_STATUS: ${{ needs.build-docker.result }}
|
||||||
UPLOAD_STATUS: ${{ needs.upload.result }}
|
UPLOAD_STATUS: ${{ needs.upload.result }}
|
||||||
|
BUILD_MSSQLMIGRATORUTILITY_STATUS: ${{ needs.build-mssqlmigratorutility.result }}
|
||||||
run: |
|
run: |
|
||||||
if [ "$CLOC_STATUS" = "failure" ]; then
|
if [ "$CLOC_STATUS" = "failure" ]; then
|
||||||
exit 1
|
exit 1
|
||||||
@ -504,6 +558,8 @@ jobs:
|
|||||||
exit 1
|
exit 1
|
||||||
elif [ "$UPLOAD_STATUS" = "failure" ]; then
|
elif [ "$UPLOAD_STATUS" = "failure" ]; then
|
||||||
exit 1
|
exit 1
|
||||||
|
elif [ "$BUILD_MSSQLMIGRATORUTILITY_STATUS" = "failure" ]; then
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Login to Azure - Prod Subscription
|
- name: Login to Azure - Prod Subscription
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio Version 16
|
# Visual Studio Version 16
|
||||||
VisualStudioVersion = 16.0.29102.190
|
VisualStudioVersion = 16.0.29102.190
|
||||||
@ -112,6 +112,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Infrastructure.IntegrationT
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqliteMigrations", "util\SqliteMigrations\SqliteMigrations.csproj", "{07143DFA-F242-47A4-A15E-39C9314D4140}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqliteMigrations", "util\SqliteMigrations\SqliteMigrations.csproj", "{07143DFA-F242-47A4-A15E-39C9314D4140}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MsSqlMigratorUtility", "util\MsSqlMigratorUtility\MsSqlMigratorUtility.csproj", "{D9A2CCBB-FB0A-4BBA-A9ED-BA9FF277C880}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
@ -23,10 +23,40 @@ public class DbMigrator
|
|||||||
}.ConnectionString;
|
}.ConnectionString;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool MigrateMsSqlDatabase(bool enableLogging = true,
|
public bool MigrateMsSqlDatabaseWithRetries(bool enableLogging = true,
|
||||||
CancellationToken cancellationToken = default(CancellationToken))
|
CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
if (enableLogging && _logger != null)
|
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)
|
||||||
{
|
{
|
||||||
_logger.LogInformation(Constants.BypassFiltersEventId, "Migrating database.");
|
_logger.LogInformation(Constants.BypassFiltersEventId, "Migrating database.");
|
||||||
}
|
}
|
||||||
@ -89,7 +119,7 @@ public class DbMigrator
|
|||||||
var upgrader = builder.Build();
|
var upgrader = builder.Build();
|
||||||
var result = upgrader.PerformUpgrade();
|
var result = upgrader.PerformUpgrade();
|
||||||
|
|
||||||
if (enableLogging && _logger != null)
|
if (_logger != null)
|
||||||
{
|
{
|
||||||
if (result.Successful)
|
if (result.Successful)
|
||||||
{
|
{
|
||||||
|
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
@ -1,7 +1,6 @@
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Net.Http.Json;
|
using System.Net.Http.Json;
|
||||||
using Bit.Migrator;
|
using Bit.Migrator;
|
||||||
using Microsoft.Data.SqlClient;
|
|
||||||
|
|
||||||
namespace Bit.Setup;
|
namespace Bit.Setup;
|
||||||
|
|
||||||
@ -187,35 +186,10 @@ public class Program
|
|||||||
|
|
||||||
private static void MigrateDatabase(int attempt = 1)
|
private static void MigrateDatabase(int attempt = 1)
|
||||||
{
|
{
|
||||||
try
|
var vaultConnectionString = Helpers.GetValueFromEnvFile("global",
|
||||||
{
|
"globalSettings__sqlServer__connectionString");
|
||||||
Helpers.WriteLine(_context, "Migrating database.");
|
var migrator = new DbMigrator(vaultConnectionString, null);
|
||||||
var vaultConnectionString = Helpers.GetValueFromEnvFile("global",
|
migrator.MigrateMsSqlDatabaseWithRetries(false);
|
||||||
"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()
|
private static bool ValidateInstallation()
|
||||||
|
Loading…
Reference in New Issue
Block a user