Initial commit
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
using MemeVault.Infrastructure.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
var host = Host.CreateDefaultBuilder(args)
|
||||
.ConfigureAppConfiguration(cfg => cfg.AddEnvironmentVariables())
|
||||
.ConfigureServices((ctx, services) =>
|
||||
{
|
||||
services.AddDbContext<AppDbContext>(opts =>
|
||||
opts.UseNpgsql(ctx.Configuration.GetConnectionString("Postgres")));
|
||||
})
|
||||
.Build();
|
||||
|
||||
var logger = host.Services.GetRequiredService<ILogger<Program>>();
|
||||
|
||||
try
|
||||
{
|
||||
logger.LogInformation("Starting database migration...");
|
||||
|
||||
using var scope = host.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
|
||||
var pending = await db.Database.GetPendingMigrationsAsync();
|
||||
var list = pending.ToList();
|
||||
|
||||
if (list.Count == 0)
|
||||
{
|
||||
logger.LogInformation("No pending migrations.");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogInformation("Applying {Count} migration(s): {Names}",
|
||||
list.Count, string.Join(", ", list));
|
||||
|
||||
await db.Database.MigrateAsync();
|
||||
logger.LogInformation("Migration completed successfully.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogCritical(ex, "Migration failed.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
Reference in New Issue
Block a user