Skip to content

Commit

Permalink
Add migrate feature [Updated]
Browse files Browse the repository at this point in the history
- Implement functionality to run initial migration before the application starts

Fixes EduardoPires#199
  • Loading branch information
doks-coders committed Mar 19, 2024
1 parent 0a6ad80 commit aabab52
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Equinox.UI.Web/Data/MigrationExtension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Equinox.Infra.Data.Context;
using Microsoft.EntityFrameworkCore;
using System;

namespace Equinox.UI.Web.Data
{
Expand All @@ -22,8 +23,8 @@ public static async Task<WebApplication> Migrate(this WebApplication app)
var equinoxContext = service.GetRequiredService<EquinoxContext>();
var eventStoreSqlContext = service.GetRequiredService<EventStoreSqlContext>();

await equinoxContext.Database.MigrateAsync();
await eventStoreSqlContext.Database.MigrateAsync();
await ApplyMigrationsAsync(equinoxContext);
await ApplyMigrationsAsync(eventStoreSqlContext);
}
catch (Exception ex)
{
Expand All @@ -33,6 +34,18 @@ public static async Task<WebApplication> Migrate(this WebApplication app)
}
return app;
}

private static async Task ApplyMigrationsAsync(DbContext context)
{
if (context.Database.GetPendingMigrations().Any())
{
await context.Database.MigrateAsync();
}
}

// Somewhere in your code...


}

}

0 comments on commit aabab52

Please sign in to comment.