Auto apply EF migrations on startup
All checks were successful
Build Docker Images / build-and-push (push) Successful in 1m25s

This commit is contained in:
Michał Zieliński
2025-10-18 10:04:47 +02:00
parent 8fd8a79d75
commit 5441e280e0

View File

@@ -83,6 +83,33 @@ builder.Services.AddCors(options =>
var app = builder.Build(); var app = builder.Build();
// Auto-apply migrations on startup
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<BimAIDbContext>();
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.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.UseSwagger(); app.UseSwagger();