The default schema step is EnsureCreated: it creates a missing database and changes nothing about an existing one. The migrate path could not rescue that database either, because a database EnsureCreated once made has no __EFMigrationsHistory, so the migration set would begin by creating tables that are already there. Same bug with two faces. An operator who upgraded kept a schema with every table except the new ones, and the pillar behind them answered as if there were nothing to see — the compose data volume outliving 'compose down' is exactly that state, which is where it was noticed. At startup the edge now lists the tables the model describes and the database lacks and creates exactly those, from the model through the provider in use, indexes included. A table that exists is never touched, so an upgrade cannot lose an operator's block list — proven on the wire, not asserted: a block written before the upgrade still refuses the same request after it, with the note the operator typed still in the listing. On the migrate path a database with no history table is baselined first: a migration is claimed as applied only when every table its target model describes is already present, and the claim stops at the first migration it cannot confirm, so a later one is never applied over an unclaimed earlier one. SchemaBaseline is that decision as a pure function; SchemaDdl is the statement filter over EF's own generated script, and an unrecognised statement is left out rather than run, because the safe failure is a table that stays missing and is reported. What happened is on /admin-api/state: schemaStrategy, schemaTablesAdded, schemaMigrationsBaselined, schemaMigrationsApplied, schemaMissingTables. The last is the invariant — non-empty names a pillar this build can run with no storage behind it, and OPENWAF_SCHEMA=none now changes nothing and reports all of it the same way. The limit is stated rather than hidden: the baseline recognises a table by its name, so a migration that alters an existing table still needs a human decision and the edge refuses to guess one. OPENWAF_SCHEMA is read through configuration instead of the environment directly. An environment variable still sets it; the change is that a test can now ask for a strategy without mutating process-wide state that parallel xUnit collections share. Gates on this tree: build 0 warnings / 0 errors, unit 240/240 (+13: the baseline claim, the statement filter), integration 230/230 in 22 s (+5: an edge started against a database with two tables dropped, the indexes arriving with the table, the operator block surviving, the baseline on the migrate path, and the operator-managed schema reporting rather than fixing), smoke passed 52 failed 0 unchanged, compose 216 passed 0 failed (+7: the same upgrade against the shipped image, where the volume really does outlive the stack), console 90 assertions 0 failed unchanged. Two things the new compose scene caught about the gate itself. The rule-write assertion used code(), which discards the body, so the first run answered 400 and the gate could not say why — the assertion now prints the body it got. And the delete looked its rule up with jget while writing jcount's loop variable, a NameError inside the helper that surfaced as a 405 on the assertion: the trap the script's own comment names, walked into anyway.
102 lines
3.4 KiB
C#
102 lines
3.4 KiB
C#
using OpenWaf.Infrastructure.Persistence;
|
|
using Xunit;
|
|
|
|
namespace OpenWaf.Tests.Unit;
|
|
|
|
/// <summary>
|
|
/// Which statements of a model-generated script may run against a database that
|
|
/// already has other tables in it (OPEN-55). Running the whole script is the
|
|
/// destructive option — it fails on the tables holding the operator's block list
|
|
/// — so the filter has to keep everything that belongs to a missing table,
|
|
/// including its indexes, and nothing that belongs to a table that is there.
|
|
/// </summary>
|
|
public class SchemaDdlTests
|
|
{
|
|
private const string Script = """
|
|
CREATE TABLE "block_entries" (
|
|
"Id" INTEGER NOT NULL CONSTRAINT "PK_block_entries" PRIMARY KEY AUTOINCREMENT,
|
|
"SourceKey" TEXT NOT NULL
|
|
);
|
|
|
|
|
|
CREATE TABLE "request_rules" (
|
|
"Id" INTEGER NOT NULL CONSTRAINT "PK_request_rules" PRIMARY KEY AUTOINCREMENT,
|
|
"Pattern" TEXT NOT NULL
|
|
);
|
|
|
|
|
|
CREATE INDEX "IX_request_rules_ExpiresAt" ON "request_rules" ("ExpiresAt");
|
|
|
|
|
|
CREATE INDEX "IX_block_entries_SourceKey" ON "block_entries" ("SourceKey");
|
|
""";
|
|
|
|
private static IReadOnlySet<string> Set(params string[] tables) => tables.ToHashSet(StringComparer.OrdinalIgnoreCase);
|
|
|
|
[Fact]
|
|
public void A_statement_is_split_at_the_semicolon_that_closes_its_line()
|
|
{
|
|
Assert.Equal(4, SchemaDdl.Statements(Script).Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_postgres_batch_separator_ends_a_statement_rather_than_joining_two()
|
|
{
|
|
var statements = SchemaDdl.Statements("""
|
|
CREATE TABLE "a" ("Id" int);
|
|
GO
|
|
CREATE INDEX "ix_b" ON "b" ("Id");
|
|
GO
|
|
""");
|
|
|
|
Assert.Equal(2, statements.Count);
|
|
Assert.Contains("CREATE TABLE \"a\"", statements[0]);
|
|
Assert.DoesNotContain("CREATE TABLE", statements[1]);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_missing_table_keeps_its_own_create_and_its_own_index()
|
|
{
|
|
var kept = SchemaDdl.StatementsFor(Script, Set("request_rules"));
|
|
|
|
Assert.Equal(2, kept.Count);
|
|
Assert.Contains("CREATE TABLE \"request_rules\"", kept[0]);
|
|
Assert.Contains("IX_request_rules_ExpiresAt", kept[1]);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_table_that_is_already_there_is_never_touched_again()
|
|
{
|
|
var kept = SchemaDdl.StatementsFor(Script, Set("request_rules"));
|
|
|
|
Assert.All(kept, statement => Assert.DoesNotContain("block_entries", statement));
|
|
}
|
|
|
|
[Fact]
|
|
public void A_constraint_added_to_a_missing_table_belongs_to_that_table()
|
|
{
|
|
var kept = SchemaDdl.StatementsFor("""
|
|
ALTER TABLE "source_streaks" ADD CONSTRAINT "FK_x" FOREIGN KEY ("a") REFERENCES "block_entries" ("Id");
|
|
""", Set("source_streaks"));
|
|
|
|
Assert.Single(kept);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_statement_this_filter_does_not_recognise_is_left_out_rather_than_run()
|
|
{
|
|
// Unrecognised means "do not run it": the safe failure is a table that
|
|
// stays missing and is reported, not a statement executed against a
|
|
// table that already holds data.
|
|
var kept = SchemaDdl.StatementsFor("INSERT INTO \"request_rules\" (\"Pattern\") VALUES ('x');", Set("request_rules"));
|
|
|
|
Assert.Empty(kept);
|
|
}
|
|
|
|
[Fact]
|
|
public void Nothing_is_kept_for_a_table_that_is_not_asked_for()
|
|
{
|
|
Assert.Empty(SchemaDdl.StatementsFor(Script, Set()));
|
|
}
|
|
}
|