mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
move migrations to migrator project
This commit is contained in:
@ -9,6 +9,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Core\Core.csproj" />
|
||||
<ProjectReference Include="..\Migrator\Migrator.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
66
src/Admin/HostedServices/DatabaseMigrationHostedService.cs
Normal file
66
src/Admin/HostedServices/DatabaseMigrationHostedService.cs
Normal file
@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core;
|
||||
using Bit.Core.Jobs;
|
||||
using Bit.Migrator;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Bit.Admin.HostedServices
|
||||
{
|
||||
public class DatabaseMigrationHostedService : IHostedService, IDisposable
|
||||
{
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly ILogger<DatabaseMigrationHostedService> _logger;
|
||||
private readonly DbMigrator _dbMigrator;
|
||||
|
||||
public DatabaseMigrationHostedService(
|
||||
GlobalSettings globalSettings,
|
||||
ILogger<DatabaseMigrationHostedService> logger,
|
||||
ILogger<DbMigrator> migratorLogger,
|
||||
ILogger<JobListener> listenerLogger)
|
||||
{
|
||||
_globalSettings = globalSettings;
|
||||
_logger = logger;
|
||||
_dbMigrator = new DbMigrator(globalSettings.SqlServer.ConnectionString, migratorLogger);
|
||||
}
|
||||
|
||||
public virtual async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var maxMigrationAttempts = 10;
|
||||
for(var i = 1; i <= maxMigrationAttempts; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbMigrator.MigrateMsSqlDatabase(true, cancellationToken);
|
||||
// TODO: Maybe flip a flag somewhere to indicate migration is complete??
|
||||
break;
|
||||
}
|
||||
catch(SqlException e)
|
||||
{
|
||||
if(i >= maxMigrationAttempts)
|
||||
{
|
||||
_logger.LogError(e, "Database failed to migrate.");
|
||||
throw e;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogError(e,
|
||||
"Database unavailable for migration. Trying again (attempt #{0})...", i + 1);
|
||||
await Task.Delay(20000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public virtual void Dispose()
|
||||
{ }
|
||||
}
|
||||
}
|
@ -75,7 +75,11 @@ namespace Bit.Admin
|
||||
// Jobs service
|
||||
Jobs.JobsHostedService.AddJobsServices(services);
|
||||
services.AddHostedService<Jobs.JobsHostedService>();
|
||||
if(!globalSettings.SelfHosted)
|
||||
if(globalSettings.SelfHosted)
|
||||
{
|
||||
services.AddHostedService<HostedServices.DatabaseMigrationHostedService>();
|
||||
}
|
||||
else
|
||||
{
|
||||
if(CoreHelpers.SettingHasValue(globalSettings.Storage.ConnectionString))
|
||||
{
|
||||
|
Reference in New Issue
Block a user