1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-11 15:17:44 +01:00
bitwarden-server/util/SqliteMigrations/SqliteDbMigrator.cs
Matt Gibson 7cbc4a8970
Add Sqlite as EF DB provider (#2487)
* Add Sqlite as EF DB provider

Note: In-memory sqlite does not work across projects, since the migrator
only runs on the Admin project

Co-authored-by: Justin Baur <justindbaur@users.noreply.github.com>

* Include example sqlite connection string

* Add migrator assembly to sqlite connection

* Update initial migration to current schema state

* dotnet format 🤖

* Update package locks

* Respect name set in BW_SSL_KEY for cert generation (#2490)

(cherry picked from commit 2469e10110)

* [PS-2016] Add ability to change UID/GID for Bitwarden unified (#2495)

(cherry picked from commit c6fbe8cc44)

* Add SqliteMigrations project to unified Dockerfile

Co-authored-by: Justin Baur <justindbaur@users.noreply.github.com>
Co-authored-by: accolon <mail@accolon.net>
Co-authored-by: Vince Grassia <593223+vgrassia@users.noreply.github.com>
2022-12-14 08:28:51 -06:00

42 lines
1.3 KiB
C#

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.SqliteMigrations;
public class SqliteDbMigrator : IDbMigrator
{
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly ILogger<SqliteDbMigrator> _logger;
public SqliteDbMigrator(IServiceScopeFactory serviceScopeFactory, ILogger<SqliteDbMigrator> 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;
}
}