Files
hrynco-notification-service/HrynCo.NotificationService.Migrator/Program.cs
T
agrynco 2757869176 feat: consume transactional email notifications
Add contract validation, SMTP delivery results, terminal failure context, neutral development seeding, and local Docker setup.

Ref: IT-1033
2026-08-04 12:32:28 +03:00

61 lines
2.0 KiB
C#

using HrynCo.NotificationService.DAL.EF;
using HrynCo.NotificationService.Migrator;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
try
{
Log.Information("🚀 Notification Service Migrator starting...");
var host = Host.CreateDefaultBuilder(args)
.UseSerilog((ctx, cfg) => cfg
.ReadFrom.Configuration(ctx.Configuration)
.WriteTo.Console())
.ConfigureServices((ctx, services) =>
{
var connectionString = ctx.Configuration["App:ConnectionString"]
?? throw new InvalidOperationException("App:ConnectionString is not configured.");
services.AddNotificationDataAccess(connectionString);
services.AddScoped<DevelopmentDataSeeder>();
})
.Build();
using var scope = host.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<NotificationDbContext>();
Log.Information("Applying migrations...");
await db.Database.MigrateAsync();
Log.Information("Migrations applied successfully.");
var environment = scope.ServiceProvider.GetRequiredService<IHostEnvironment>();
if (environment.IsDevelopment())
{
string serviceName = host.Services.GetRequiredService<IConfiguration>()["DevelopmentSeed:ServiceName"]
?? DevelopmentDataSeeder.DefaultServiceName;
Log.Information("Seeding development email configuration...");
var seeder = scope.ServiceProvider.GetRequiredService<DevelopmentDataSeeder>();
await seeder.SeedAsync(serviceName);
Log.Information("Development email configuration is ready.");
}
}
catch (Exception ex)
{
Log.Fatal(ex, "Migration failed.");
return 1;
}
finally
{
await Log.CloseAndFlushAsync();
}
return 0;