From 34e978a482a405b018c4c475d576d46978de1aa0 Mon Sep 17 00:00:00 2001 From: Jerry Phillips Date: Sat, 23 May 2026 15:07:05 -0400 Subject: [PATCH] fix(#251): prevent startup crash when database migration fails on staging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary If MigrateAsync() throws at startup (e.g. transient DB connectivity issue, schema mismatch on staging), the entire process was crashing before UseCors ran — causing 503 on ALL endpoints and no CORS headers on any response. Wrap MigrateAsync in try-catch so the app starts and serves requests even if the migration step fails. The error is logged for ops visibility. ## Changes ### API - Program.cs: wrap startup MigrateAsync in try-catch with error logging --- JobFlow.API/Program.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/JobFlow.API/Program.cs b/JobFlow.API/Program.cs index 915810f..37c174d 100644 --- a/JobFlow.API/Program.cs +++ b/JobFlow.API/Program.cs @@ -648,9 +648,17 @@ static JsonDocument ParseFirebaseAdminSdkJson(string json) using (var scope = app.Services.CreateScope()) { - var dbContextFactory = scope.ServiceProvider.GetRequiredService>(); - await using var dbContext = await dbContextFactory.CreateDbContextAsync(); - await dbContext.Database.MigrateAsync(); + var startupLogger = scope.ServiceProvider.GetRequiredService>(); + try + { + var dbContextFactory = scope.ServiceProvider.GetRequiredService>(); + await using var dbContext = await dbContextFactory.CreateDbContextAsync(); + await dbContext.Database.MigrateAsync(); + } + catch (Exception ex) + { + startupLogger.LogError(ex, "Database migration failed on startup. The application will continue but some features may be unavailable until migrations are applied."); + } } StripeConfiguration.ApiKey = builder.Configuration["StripeSettings-ApiKey"];