Files
Pavlo Ratushnyi 871eac48a5 an upgrade adds the tables a database predates, and says what it added
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.
2026-10-04 22:51:58 +02:00

85 lines
3.5 KiB
C#

using OpenWaf.Infrastructure.Persistence;
using Xunit;
namespace OpenWaf.Tests.Unit;
/// <summary>
/// The claim a baseline is allowed to make (OPEN-55). A database that has no
/// migration history can only be migrated if the migrations that already ran are
/// written down first, and writing down one that did not run is how an upgrade
/// silently skips a table the operator is waiting for.
/// </summary>
public class SchemaBaselineTests
{
private static readonly SchemaBaseline.Migration Initial = new(
"20261001002803_InitialCreate",
["block_entries", "allow_entries", "country_rules", "edge_alerts", "source_observations", "policy_settings"]);
private static readonly SchemaBaseline.Migration Rules = new(
"20261004171633_AddRequestRules",
["block_entries", "allow_entries", "country_rules", "edge_alerts", "source_observations", "policy_settings",
"request_rules", "source_streaks"]);
private static IReadOnlySet<string> Set(params string[] tables) => tables.ToHashSet(StringComparer.OrdinalIgnoreCase);
[Fact]
public void An_empty_database_claims_nothing_because_the_migrations_describe_it_from_the_start()
{
Assert.Empty(SchemaBaseline.Choose([Initial, Rules], Set()));
}
[Fact]
public void A_database_that_predates_a_table_claims_only_the_migration_that_ran()
{
var claimed = SchemaBaseline.Choose([Initial, Rules], Set(
"block_entries", "allow_entries", "country_rules", "edge_alerts", "source_observations", "policy_settings"));
Assert.Equal([Initial.Id], claimed);
}
[Fact]
public void A_database_at_the_current_schema_claims_every_migration_so_the_migration_is_a_no_op()
{
var claimed = SchemaBaseline.Choose([Initial, Rules], Set(
"block_entries", "allow_entries", "country_rules", "edge_alerts", "source_observations", "policy_settings",
"request_rules", "source_streaks"));
Assert.Equal([Initial.Id, Rules.Id], claimed);
}
[Fact]
public void The_claim_stops_at_the_first_migration_it_cannot_confirm_so_a_later_one_is_never_claimed_alone()
{
// A database holding only what the second migration added cannot have
// had the first one run: claiming the second would let the first be
// skipped forever.
var claimed = SchemaBaseline.Choose([Initial, Rules], Set("request_rules", "source_streaks"));
Assert.Empty(claimed);
}
[Fact]
public void Table_names_are_matched_the_way_the_provider_returns_them()
{
var claimed = SchemaBaseline.Choose([Initial, Rules], Set(
"BLOCK_ENTRIES", "Allow_Entries", "Country_Rules", "Edge_Alerts", "Source_Observations", "Policy_Settings"));
Assert.Equal([Initial.Id], claimed);
}
[Fact]
public void A_migration_that_adds_nothing_of_its_own_is_still_recognised_by_the_model_it_describes()
{
// The table set is the migration's target model, not the operations it
// performs, so a data-only or index-only migration in the middle of the
// set does not break the chain.
var backfill = new SchemaBaseline.Migration("20261005000000_BackfillNotes", Rules.Tables);
var claimed = SchemaBaseline.Choose([Initial, Rules, backfill], Set(
"block_entries", "allow_entries", "country_rules", "edge_alerts", "source_observations", "policy_settings",
"request_rules", "source_streaks"));
Assert.Equal([Initial.Id, Rules.Id, backfill.Id], claimed);
}
}