From 565da4fab54f25e107fd40ebf334fc83c308010a Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Thu, 1 Oct 2026 02:32:12 +0200 Subject: [PATCH] Add EF Core migrations; make schema strategy explicit - dotnet-ef 10.0.0 as a local tool manifest, InitialCreate generated against PostgreSQL (the provider a real deployment runs) via a design-time factory, so migrations never need to boot the edge - EdgeStateWarmer picks the strategy: migrate for postgres, ensurecreated for the sqlite default, none when the operator owns the schema. A migration set is provider-shaped SQL and is not applied across providers - README documents the schema story Regressions checked: 36 unit + 14 integration tests, scripts/smoke.sh 19/19. --- README.md | 15 + .../OpenWaf.Edge/Startup/EdgeStateWarmer.cs | 24 +- .../OpenWaf.Infrastructure.csproj | 4 + .../20261001002803_InitialCreate.Designer.cs | 260 ++++++++++++++++++ .../20261001002803_InitialCreate.cs | 174 ++++++++++++ .../OpenWafDbContextModelSnapshot.cs | 257 +++++++++++++++++ .../Persistence/OpenWafDbContextFactory.cs | 33 +++ dotnet/dotnet-tools.json | 13 + 8 files changed, 777 insertions(+), 3 deletions(-) create mode 100644 dotnet/OpenWaf.Infrastructure/Persistence/Migrations/20261001002803_InitialCreate.Designer.cs create mode 100644 dotnet/OpenWaf.Infrastructure/Persistence/Migrations/20261001002803_InitialCreate.cs create mode 100644 dotnet/OpenWaf.Infrastructure/Persistence/Migrations/OpenWafDbContextModelSnapshot.cs create mode 100644 dotnet/OpenWaf.Infrastructure/Persistence/OpenWafDbContextFactory.cs create mode 100644 dotnet/dotnet-tools.json diff --git a/README.md b/README.md index d40771d..2325689 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,21 @@ runtime through `PUT /admin-api/policy`, which persists to `policy_settings`. Database: `Database:Provider` = `sqlite` (default) or `postgres`, `ConnectionStrings__OpenWaf`. GeoIP: `GEOIP_MAXMIND_DB_PATH`. +## Schema + +Migrations are the schema of record for PostgreSQL, which is the provider a +real deployment runs: + +```bash +cd dotnet +dotnet ef migrations add --project OpenWaf.Infrastructure --startup-project OpenWaf.Infrastructure +``` + +A migration set is provider-shaped SQL, so it is not applied to the SQLite +default — there the schema is created from the model at startup. `OPENWAF_SCHEMA` +overrides the choice: `migrate`, `ensurecreated`, or `none` when an operator +manages the schema themselves. + ## Admin API Requires `X-Admin-Token` (or `?token=`). **No token configured means the admin diff --git a/dotnet/OpenWaf.Edge/Startup/EdgeStateWarmer.cs b/dotnet/OpenWaf.Edge/Startup/EdgeStateWarmer.cs index 053b00b..7f2992f 100644 --- a/dotnet/OpenWaf.Edge/Startup/EdgeStateWarmer.cs +++ b/dotnet/OpenWaf.Edge/Startup/EdgeStateWarmer.cs @@ -22,10 +22,28 @@ internal sealed class EdgeStateWarmer( using var scope = scopeFactory.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); - var provider = Environment.GetEnvironmentVariable("OPENWAF_DB_PROVIDER"); - if (!string.Equals(provider, "external", StringComparison.OrdinalIgnoreCase)) + // Migrations are the schema of record for the provider that actually + // runs in production — PostgreSQL. A migration set is provider-shaped + // SQL, so it is not applied to the SQLite default; there the schema is + // created from the model instead. OPENWAF_SCHEMA=none leaves both alone + // when an operator manages the schema themselves. + var isSqlite = string.Equals( + Environment.GetEnvironmentVariable("Database__Provider") ?? "sqlite", + "sqlite", StringComparison.OrdinalIgnoreCase); + var strategy = Environment.GetEnvironmentVariable("OPENWAF_SCHEMA") + ?? (isSqlite ? "ensurecreated" : "migrate"); + + switch (strategy.ToLowerInvariant()) { - await db.Database.EnsureCreatedAsync(cancellationToken); + case "ensurecreated": + await db.Database.EnsureCreatedAsync(cancellationToken); + break; + case "migrate": + await db.Database.MigrateAsync(cancellationToken); + break; + default: + log.LogInformation("edge: schema management left to the operator (OPENWAF_SCHEMA={Strategy})", strategy); + break; } await state.WarmUpAsync(cancellationToken); diff --git a/dotnet/OpenWaf.Infrastructure/OpenWaf.Infrastructure.csproj b/dotnet/OpenWaf.Infrastructure/OpenWaf.Infrastructure.csproj index baffb52..0fbae39 100644 --- a/dotnet/OpenWaf.Infrastructure/OpenWaf.Infrastructure.csproj +++ b/dotnet/OpenWaf.Infrastructure/OpenWaf.Infrastructure.csproj @@ -9,6 +9,10 @@ + + runtime; build; native; contentfiles; analyzers; buildtransitive + all +