/// <inheritdoc /> public async Task Initialize(CancellationToken cancellationToken) { if (DatabaseConfiguration.DropDatabase) { Logger.LogCritical("DropDatabase configuration option set! Dropping any existing database..."); await Database.EnsureDeletedAsync(cancellationToken).ConfigureAwait(false); } var migrations = await Database.GetAppliedMigrationsAsync(cancellationToken).ConfigureAwait(false); var wasEmpty = !migrations.Any(); if (wasEmpty || (await Database.GetPendingMigrationsAsync(cancellationToken).ConfigureAwait(false)).Any()) { Logger.LogInformation("Migrating database..."); await Database.MigrateAsync(cancellationToken).ConfigureAwait(false); } else { Logger.LogDebug("No migrations to apply."); } wasEmpty |= (await Users.CountAsync(cancellationToken).ConfigureAwait(false)) == 0; if (wasEmpty) { Logger.LogInformation("Seeding database..."); await databaseSeeder.SeedDatabase(this, cancellationToken).ConfigureAwait(false); } else if (DatabaseConfiguration.ResetAdminPassword) { Logger.LogWarning("Enabling and resetting admin password due to configuration!"); await databaseSeeder.ResetAdminPassword(this, cancellationToken).ConfigureAwait(false); } }
public Result Handle(SeedDatabaseCommand command) { try { _databaseSeeder.SeedDatabase(_unitOfWork); _unitOfWork.Complete(); return(Result.Ok()); } catch (Exception e) { return(Result.Fail(e.Message)); } }
/// <inheritdoc /> public async Task Initialize(CancellationToken cancellationToken) { Logger.LogInformation("Migrating database..."); var wasEmpty = false; if (databaseConfiguration.NoMigrations) { Logger.LogWarning("Using all or nothing migration strategy!"); await Database.EnsureCreatedAsync(cancellationToken).ConfigureAwait(false); } else { var migrations = await Database.GetAppliedMigrationsAsync(cancellationToken).ConfigureAwait(false); wasEmpty = !migrations.Any(); await Database.MigrateAsync(cancellationToken).ConfigureAwait(false); } wasEmpty |= (await Users.CountAsync(cancellationToken).ConfigureAwait(false)) == 0; if (wasEmpty) { Logger.LogInformation("Seeding database..."); await databaseSeeder.SeedDatabase(this, cancellationToken).ConfigureAwait(false); } else { Logger.LogDebug("No migrations applied!"); if (databaseConfiguration.ResetAdminPassword) { Logger.LogWarning("Enabling and resetting admin password due to configuration!"); await databaseSeeder.ResetAdminPassword(this, cancellationToken).ConfigureAwait(false); } } }