| | | 1 | | using Anichron.API.Services; |
| | | 2 | | using Anichron.API.Settings; |
| | | 3 | | using Anichron.Core.Data; |
| | | 4 | | using Anichron.Infrastructure.Configuration; |
| | | 5 | | using Anichron.Infrastructure.Data; |
| | | 6 | | |
| | | 7 | | namespace Anichron.API.Infrastructure; |
| | | 8 | | |
| | | 9 | | public static partial class DatabaseStartupExtensions |
| | | 10 | | { |
| | | 11 | | public const string MigrateCommand = "migrate"; |
| | | 12 | | |
| | | 13 | | public static async Task MigrateAndSeedAsync(string[] args) |
| | 0 | 14 | | { |
| | 0 | 15 | | using var hostSettings = new ConfigurationManager(); |
| | 0 | 16 | | hostSettings.AddEnvironmentVariables("ASPNETCORE_"); |
| | 0 | 17 | | var builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings { Args = args, Configuration = ho |
| | 0 | 18 | | builder.AddAppConfiguration(ApiIniDefaults.Entries); |
| | 0 | 19 | | builder.Services.AddDatabase(builder.Configuration).AddBootstrapAdmin(builder.Configuration); |
| | | 20 | | |
| | 0 | 21 | | using var host = builder.Build(); |
| | 0 | 22 | | await host.StartAsync(); |
| | 0 | 23 | | await host.MigrateAndSeedDatabaseAsync(host.Services.GetRequiredService<IHostApplicationLifetime>().ApplicationS |
| | 0 | 24 | | await host.StopAsync(); |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | extension(IHost host) |
| | | 28 | | { |
| | | 29 | | private async Task MigrateAndSeedDatabaseAsync(CancellationToken ct) |
| | 0 | 30 | | { |
| | 0 | 31 | | var logger = host.Services.GetRequiredService<ILoggerFactory>().CreateLogger(typeof(DatabaseStartupExtension |
| | 0 | 32 | | await new DatabaseStartupRetry( |
| | 0 | 33 | | TimeProvider.System, |
| | 0 | 34 | | AppDefaults.Startup.MaxDbRetryAttempts, |
| | 0 | 35 | | TimeSpan.FromSeconds(AppDefaults.Startup.DbRetryDelaySeconds)) |
| | 0 | 36 | | .RunAsync( |
| | 0 | 37 | | host.MigrateOnceAsync, |
| | 0 | 38 | | (ex, attempt) => Log.DatabaseNotReady(logger, ex, attempt, |
| | 0 | 39 | | AppDefaults.Startup.MaxDbRetryAttempts, AppDefaults.Startup.DbRetryDelaySeconds), |
| | 0 | 40 | | ex => Log.DatabaseUnavailable(logger, ex, AppDefaults.Startup.MaxDbRetryAttempts), |
| | 0 | 41 | | ct); |
| | 0 | 42 | | await host.SeedAsync(ct); |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | private async Task MigrateOnceAsync(CancellationToken ct) |
| | 0 | 46 | | { |
| | 0 | 47 | | await using var scope = host.Services.CreateAsyncScope(); |
| | 0 | 48 | | var dbContext = scope.ServiceProvider.GetRequiredService<AnichronDbContext>(); |
| | 0 | 49 | | var logger = scope.ServiceProvider.GetRequiredService<ILogger<AnichronDbContext>>(); |
| | | 50 | | |
| | 0 | 51 | | await dbContext.Database.MigrateWithAdvisoryLockAsync(ct); |
| | 0 | 52 | | Log.MigrationApplied(logger); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | private async Task SeedAsync(CancellationToken ct) |
| | 0 | 56 | | { |
| | 0 | 57 | | await using var scope = host.Services.CreateAsyncScope(); |
| | 0 | 58 | | var bootstrapSeeder = scope.ServiceProvider.GetRequiredService<IBootstrapSeeder>(); |
| | 0 | 59 | | await bootstrapSeeder.SeedAsync(ct); |
| | | 60 | | |
| | 0 | 61 | | var adminResetService = scope.ServiceProvider.GetRequiredService<IBootstrapResetService>(); |
| | 0 | 62 | | await adminResetService.ResetIfRequestedAsync(ct); |
| | 0 | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | private static partial class Log |
| | | 67 | | { |
| | | 68 | | [LoggerMessage(Level = LogLevel.Information, Message = "Database migration applied successfully.")] |
| | | 69 | | public static partial void MigrationApplied(ILogger logger); |
| | | 70 | | |
| | | 71 | | [LoggerMessage(Level = LogLevel.Warning, Message = "Database not ready (attempt {Attempt}/{Max}). Retrying in {D |
| | | 72 | | public static partial void DatabaseNotReady(ILogger logger, Exception ex, int attempt, int max, int delay); |
| | | 73 | | |
| | | 74 | | [LoggerMessage(Level = LogLevel.Error, Message = "Database unavailable after {Max} attempts. Aborting.")] |
| | | 75 | | public static partial void DatabaseUnavailable(ILogger logger, Exception ex, int max); |
| | | 76 | | } |
| | | 77 | | } |