diff --git a/src/Backend/DiunaBI.API/Program.cs b/src/Backend/DiunaBI.API/Program.cs index 5ec7685..dbe741c 100644 --- a/src/Backend/DiunaBI.API/Program.cs +++ b/src/Backend/DiunaBI.API/Program.cs @@ -99,6 +99,33 @@ builder.Services.AddSingleton(); var app = builder.Build(); +// Auto-apply migrations on startup +using (var scope = app.Services.CreateScope()) +{ + var db = scope.ServiceProvider.GetRequiredService(); + + try + { + var pending = await db.Database.GetPendingMigrationsAsync(); + if (pending.Any()) + { + app.Logger.LogWarning("Applying {Count} pending migrations: {List}", + pending.Count(), string.Join(", ", pending)); + await db.Database.MigrateAsync(); + app.Logger.LogInformation("Migrations applied successfully."); + } + else + { + app.Logger.LogInformation("No pending migrations."); + } + } + catch (Exception ex) + { + app.Logger.LogCritical(ex, "Migration failed - application will not start."); + throw; // stop startup + } +} + if (app.Environment.IsProduction()) { app.UseSerilogRequestLogging(options =>