diff --git a/README.md b/README.md index 0dd05e9..32d76d0 100644 --- a/README.md +++ b/README.md @@ -345,6 +345,15 @@ happened is on `/admin-api/state`: `schemaStrategy`, `schemaTablesAdded`, can run with no storage behind it. `none` changes nothing and reports all of it the same way. +Measured on postgres rather than assumed: a database created by `ensurecreated` +and then asked to `migrate` gained `request_rules` and `source_streaks` — one +migration baselined, one applied, both rows in the history table, the indexes +present. That run is also what caught a trap in EF itself: +`IHistoryRepository.Exists()` answers **true** on postgres for a database that +has tables and no history table at all — the one state this step exists to +notice — while the same call answers false on sqlite. The question is therefore +asked of the table listing, the answer both providers agree on. + ## Admin API Requires `X-Admin-Token` (or `?token=`). **No token configured means the admin diff --git a/dotnet/OpenWaf.Infrastructure/Persistence/SchemaUpgrader.cs b/dotnet/OpenWaf.Infrastructure/Persistence/SchemaUpgrader.cs index 1a1876c..9b7c47f 100644 --- a/dotnet/OpenWaf.Infrastructure/Persistence/SchemaUpgrader.cs +++ b/dotnet/OpenWaf.Infrastructure/Persistence/SchemaUpgrader.cs @@ -99,10 +99,26 @@ public static class SchemaUpgrader public static async Task BaselineIfUntrackedAsync(OpenWafDbContext db, ILogger log, CancellationToken ct = default) { var history = db.GetService(); - if (await history.ExistsAsync(ct)) return 0; + // Asked of the table listing rather than of IHistoryRepository.Exists: + // measured on postgres, Exists() answered true for a database that has + // tables and no history table at all — which is exactly the state this + // method exists to notice. On sqlite the same call answered false. The + // listing is the one answer both providers agree on. var existing = await ExistingTablesAsync(db, ct); - if (existing.Count == 0) return 0; + if (existing.Contains(HistoryRepository.DefaultTableName)) return 0; + + if (existing.Count == 0) + { + // Either a first install, which the migrations describe from the + // beginning, or a listing this provider does not answer — and an + // empty listing is the one answer that would make an upgrade + // destructive, so it is said out loud rather than treated as "empty". + log.LogWarning( + "edge: the table listing for this database came back empty while it has no migration history — " + + "nothing can be baselined, so the migrations will run against it and report what they find."); + return 0; + } var assembly = db.GetService(); var migrations = new List();