< Summary

Information
Class: Anichron.API.Infrastructure.DatabaseStartupExtensions
Assembly: Anichron.API
File(s): /home/runner/work/anichron/anichron/src/Anichron.API/Infrastructure/DatabaseStartupExtensions.cs
Tag: 19_45211875487
Line coverage
0%
Covered lines: 0
Uncovered lines: 39
Coverable lines: 39
Total lines: 77
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MigrateAndSeedAsync()100%210%
MigrateAndSeedDatabaseAsync()100%210%
MigrateOnceAsync()100%210%
SeedAsync()100%210%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.API/Infrastructure/DatabaseStartupExtensions.cs

#LineLine coverage
 1using Anichron.API.Services;
 2using Anichron.API.Settings;
 3using Anichron.Core.Data;
 4using Anichron.Infrastructure.Configuration;
 5using Anichron.Infrastructure.Data;
 6
 7namespace Anichron.API.Infrastructure;
 8
 9public static partial class DatabaseStartupExtensions
 10{
 11    public const string MigrateCommand = "migrate";
 12
 13    public static async Task MigrateAndSeedAsync(string[] args)
 014    {
 015        using var hostSettings = new ConfigurationManager();
 016        hostSettings.AddEnvironmentVariables("ASPNETCORE_");
 017        var builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings { Args = args, Configuration = ho
 018        builder.AddAppConfiguration(ApiIniDefaults.Entries);
 019        builder.Services.AddDatabase(builder.Configuration).AddBootstrapAdmin(builder.Configuration);
 20
 021        using var host = builder.Build();
 022        await host.StartAsync();
 023        await host.MigrateAndSeedDatabaseAsync(host.Services.GetRequiredService<IHostApplicationLifetime>().ApplicationS
 024        await host.StopAsync();
 025    }
 26
 27    extension(IHost host)
 28    {
 29        private async Task MigrateAndSeedDatabaseAsync(CancellationToken ct)
 030        {
 031            var logger = host.Services.GetRequiredService<ILoggerFactory>().CreateLogger(typeof(DatabaseStartupExtension
 032            await new DatabaseStartupRetry(
 033                    TimeProvider.System,
 034                    AppDefaults.Startup.MaxDbRetryAttempts,
 035                    TimeSpan.FromSeconds(AppDefaults.Startup.DbRetryDelaySeconds))
 036                .RunAsync(
 037                    host.MigrateOnceAsync,
 038                    (ex, attempt) => Log.DatabaseNotReady(logger, ex, attempt,
 039                        AppDefaults.Startup.MaxDbRetryAttempts, AppDefaults.Startup.DbRetryDelaySeconds),
 040                    ex => Log.DatabaseUnavailable(logger, ex, AppDefaults.Startup.MaxDbRetryAttempts),
 041                    ct);
 042            await host.SeedAsync(ct);
 043        }
 44
 45        private async Task MigrateOnceAsync(CancellationToken ct)
 046        {
 047            await using var scope = host.Services.CreateAsyncScope();
 048            var dbContext = scope.ServiceProvider.GetRequiredService<AnichronDbContext>();
 049            var logger = scope.ServiceProvider.GetRequiredService<ILogger<AnichronDbContext>>();
 50
 051            await dbContext.Database.MigrateWithAdvisoryLockAsync(ct);
 052            Log.MigrationApplied(logger);
 053        }
 54
 55        private async Task SeedAsync(CancellationToken ct)
 056        {
 057            await using var scope = host.Services.CreateAsyncScope();
 058            var bootstrapSeeder = scope.ServiceProvider.GetRequiredService<IBootstrapSeeder>();
 059            await bootstrapSeeder.SeedAsync(ct);
 60
 061            var adminResetService = scope.ServiceProvider.GetRequiredService<IBootstrapResetService>();
 062            await adminResetService.ResetIfRequestedAsync(ct);
 063        }
 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}