diff --git a/HrynCo.NotificationService.Migrator/Dockerfile b/HrynCo.NotificationService.Migrator/Dockerfile new file mode 100644 index 0000000..f34c4b4 --- /dev/null +++ b/HrynCo.NotificationService.Migrator/Dockerfile @@ -0,0 +1,17 @@ +FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build +WORKDIR /src + +COPY ["HrynCo.NotificationService.Migrator/HrynCo.NotificationService.Migrator.csproj", "HrynCo.NotificationService.Migrator/"] +COPY ["HrynCo.NotificationService.DAL.EF/HrynCo.NotificationService.DAL.EF.csproj", "HrynCo.NotificationService.DAL.EF/"] +COPY ["HrynCo.NotificationService.DAL.Abstract/HrynCo.NotificationService.DAL.Abstract.csproj", "HrynCo.NotificationService.DAL.Abstract/"] +COPY ["Directory.Packages.props", "./"] +COPY ["Directory.Build.props", "./"] +RUN dotnet restore "HrynCo.NotificationService.Migrator/HrynCo.NotificationService.Migrator.csproj" + +COPY . . +RUN dotnet publish "HrynCo.NotificationService.Migrator/HrynCo.NotificationService.Migrator.csproj" -c Release -o /app/publish --no-restore + +FROM mcr.microsoft.com/dotnet/runtime:10.0 +WORKDIR /app +COPY --from=build /app/publish . +ENTRYPOINT ["dotnet", "HrynCo.NotificationService.Migrator.dll"] diff --git a/HrynCo.NotificationService.Migrator/HrynCo.NotificationService.Migrator.csproj b/HrynCo.NotificationService.Migrator/HrynCo.NotificationService.Migrator.csproj new file mode 100644 index 0000000..080e9b3 --- /dev/null +++ b/HrynCo.NotificationService.Migrator/HrynCo.NotificationService.Migrator.csproj @@ -0,0 +1,25 @@ + + + + Exe + net10.0 + enable + enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + diff --git a/HrynCo.NotificationService.Migrator/Program.cs b/HrynCo.NotificationService.Migrator/Program.cs new file mode 100644 index 0000000..d3e979d --- /dev/null +++ b/HrynCo.NotificationService.Migrator/Program.cs @@ -0,0 +1,46 @@ +using HrynCo.NotificationService.DAL.EF; +using Microsoft.EntityFrameworkCore; +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.AddDbContext(options => + options.UseNpgsql(connectionString)); + }) + .Build(); + + using var scope = host.Services.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + + Log.Information("Applying migrations..."); + await db.Database.MigrateAsync(); + Log.Information("Migrations applied successfully."); +} +catch (Exception ex) +{ + Log.Fatal(ex, "Migration failed."); + return 1; +} +finally +{ + await Log.CloseAndFlushAsync(); +} + +return 0; diff --git a/HrynCo.NotificationService.Migrator/appsettings.json b/HrynCo.NotificationService.Migrator/appsettings.json new file mode 100644 index 0000000..530da5d --- /dev/null +++ b/HrynCo.NotificationService.Migrator/appsettings.json @@ -0,0 +1,18 @@ +{ + "App": { + "ConnectionString": "" + }, + "Serilog": { + "MinimumLevel": { + "Default": "Information", + "Override": { + "Microsoft": "Warning", + "Microsoft.EntityFrameworkCore": "Information" + } + }, + "WriteTo": [ + { "Name": "Console" } + ], + "Enrich": [ "FromLogContext" ] + } +} diff --git a/HrynCo.NotificationService.slnx b/HrynCo.NotificationService.slnx index 9becdfd..0bdcbeb 100644 --- a/HrynCo.NotificationService.slnx +++ b/HrynCo.NotificationService.slnx @@ -8,7 +8,11 @@ + + + + diff --git a/docker/environments/docker-compose.yml b/docker/environments/docker-compose.yml index e44dc0c..59e5c91 100644 --- a/docker/environments/docker-compose.yml +++ b/docker/environments/docker-compose.yml @@ -1,18 +1,13 @@ services: migrator: - image: mcr.microsoft.com/dotnet/sdk:10.0 - working_dir: /src - volumes: - - ../..:/src + build: + context: ../.. + dockerfile: HrynCo.NotificationService.Migrator/Dockerfile environment: - App__ConnectionString=${CONNECTION_STRING} depends_on: db: condition: service_started - command: - - /bin/sh - - -c - - dotnet tool install --tool-path /tmp/dotnet-tools dotnet-ef --version "9.*" && until /tmp/dotnet-tools/dotnet-ef database update --project /src/HrynCo.NotificationService.DAL.EF --startup-project /src/HrynCo.NotificationService.Web; do echo "Migration failed, retrying in 3s..."; sleep 3; done restart: "no" api: