You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.3 KiB
41 lines
1.3 KiB
using Bit.Core; |
|
using Bit.Core.Utilities; |
|
using Bit.Infrastructure.EntityFramework.Repositories; |
|
using Microsoft.EntityFrameworkCore; |
|
using Microsoft.Extensions.DependencyInjection; |
|
using Microsoft.Extensions.Logging; |
|
|
|
namespace Bit.PostgresMigrations; |
|
|
|
public class PostgresDbMigrator : IDbMigrator |
|
{ |
|
private readonly IServiceScopeFactory _serviceScopeFactory; |
|
private readonly ILogger<PostgresDbMigrator> _logger; |
|
|
|
public PostgresDbMigrator(IServiceScopeFactory serviceScopeFactory, ILogger<PostgresDbMigrator> logger) |
|
{ |
|
_serviceScopeFactory = serviceScopeFactory; |
|
_logger = logger; |
|
} |
|
|
|
public bool MigrateDatabase(bool enableLogging = true, |
|
CancellationToken cancellationToken = default(CancellationToken)) |
|
{ |
|
if (enableLogging && _logger != null) |
|
{ |
|
_logger.LogInformation(Constants.BypassFiltersEventId, "Migrating database."); |
|
} |
|
|
|
using var scope = _serviceScopeFactory.CreateScope(); |
|
var databaseContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>(); |
|
databaseContext.Database.Migrate(); |
|
|
|
if (enableLogging && _logger != null) |
|
{ |
|
_logger.LogInformation(Constants.BypassFiltersEventId, "Migration successful."); |
|
} |
|
|
|
cancellationToken.ThrowIfCancellationRequested(); |
|
return true; |
|
} |
|
}
|
|
|