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 +