Skip to content

Catalog verifier: the kernel's part, existence and what the catalog states in columns of its own - #265

Merged
CaffeinatedCoder merged 4 commits into
mainfrom
feat/256-catalog-verifier
Nov 29, 2026
Merged

CaffeinatedCoder merged 4 commits into
mainfrom
feat/256-catalog-verifier

Conversation

@CaffeinatedCoder

@CaffeinatedCoder CaffeinatedCoder commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Closes #256

Problem

Nothing could say whether a database holds what a model declares. A database migrated by a context without UseSchemaKit(), by a differ a design-time factory put in, or altered by hand has the tables and a migrations history that says the model was applied, and none or other of the declared indexes and constraints. Every guard there is asks the pipeline; none asks the database.

Change

Kernel

  • context.Database.VerifySchemaKitAsync(cancellationToken) and what a consumer reads from it, none of it marked: SchemaVerificationReport (Objects, IsClean, NotPresent), VerifiedObject (entity type, owner, kind, name, table, finding), CatalogFinding as a closed union of Present, Missing, Different and Unverifiable, and CatalogDifference (facet, declared, found). Different holds every facet that differs, not the first
  • In the contract (SK9001): ICatalogProbe, which a contributor implements on its own class, and CatalogProbeContext (the context's connection, open, and its transaction). A contributor without one is Unverifiable for each of its objects. The kernel refuses an answer that is not one finding for each object
  • The planner's reading of a model moved, unchanged, into DeclaredObjects: the planner asks it for both sides of a diff, and the verifier for the context's own design-time model. One declaration is one object in each table it lands on, to both
  • The planner's two refusals for the consumer's own model moved there with it, as RequireNames and RequireDependencies, and the verifier asks both through RequireSound: a name taken twice (SK1001), and an object that needs one the model does not declare. The planner asks them at the two moments of a diff, as before
  • Nothing in the kernel names a catalog. Its tests read SQLite's through a sample probe written against the public contract

PostgreSQL package

  • PgCatalog reads an index, or a constraint with its index, from pg_index, pg_class and pg_constraint, in one query for all objects of a kind. PgCatalogProbe compares: the table, the access method, uniqueness and NULLS NOT DISTINCT, each key that is a column with its order, operator class and collation, the included columns, the storage parameters by what they mean, whether there is a condition, a constraint's operators and deferral, and whether the index is valid
  • The two descriptors hold their keys as values (PgIndexKey, PgExclusionElement, PgStorageParameter), and Create writes the SQL from them. No statement and no stored form changed: the tests that hold the statements verbatim pass as they were. What makes two keys one object did change, in one case: see "Decided here", point 4
  • An expression key and a condition are not compared here: an object that has one and agrees in everything else is Unverifiable with the part named. Comparing them is Catalog verifier: expression keys and conditions, compared through a shadow #259, by the way decided on Catalog verifier: VerifySchemaKitAsync for the PostgreSQL kinds #30 on 2026-11-28 from spike G (Spike G: can the server say what a declared expression is, for the verifier? #255)
  • A declared expression is never judged against the catalog's columns. SQL written by hand that is one column in parentheses is a column in the catalog, and 5.x's fixture has such an index (ux_account_kind_title). A declared column where the catalog has an expression is a difference; the other way round is not compared, so Unverifiable here and the shadow's to say in Catalog verifier: expression keys and conditions, compared through a shadow #259
  • A role the catalog's tables were taken from gets Unverifiable for every object. Inside a transaction of the caller's the reading is a savepoint, and every failure is undone to it, not the refusal alone
  • ICatalogProbe's remarks say that a probe has to find out whether the role can see the catalog where the database does not refuse: SQL Server's catalog views leave rows out without an error (for SqlServerIndex: clustered and nonclustered, typed #29)

Decided here, for the maintainer to confirm

  • The model read is the design-time model, also beside a compiled model, where EF Core builds it from OnModelCreating
  • A table without a schema is looked for where the session's search path finds it, not in public: the statements SchemaKit writes for such a table resolve that way
  • An index of the declared name on another table, and a constraint's own index under the name of a declared index, are Different (table, kind), not Missing
  • What makes two keys one object moved. A key is held without the parentheses a statement puts around an expression, and a fragment compares by its text. So pg.Key(x => x.GranteeId) and pg.Key(x => Sql.Raw<int>("\"GranteeId\"")) under one name are one index to the planner, and the same for an element of a constraint: main scaffolds a drop and a create for a model that changes from one to the other, this branch nothing, both ways. That is what the catalog says: PostgreSQL keeps ("GranteeId") as the column, and the two are one index there. What they read still differs, and so does a rename of the column: under the column nothing is written for the index, under the SQL the object is dropped before the rename and created after it, with the SQL as written (which fails with 42703 when it still names the old column, as for any SQL written by hand since A renamed column under SQL written by hand: the migration applies and the model's SQL names a column that is gone #209). ColumnByHandIdentityTests holds the empty diff, the two statements and the two renames, for a key and for an element. The changelog has it under "Changed"

Not in this PR: expressions and conditions (#259), and the verifier in the consumer smoke test, in the live tests' own oracle and in the READMEs (#268). SQL Server and SQLite have no probe; the contract was judged for them and measured on SQLite alone.

Not measured: a migration applied under one search_path and verified under another, which by the rule above shows as Missing.

After the review, one more commit, found while the next slice asked the verifier after every live migration: the reader's query was compiled by the server's JIT on every call. The planner prices it far above jit_above_cost, and a server as installed took 0.7 seconds to compile some 260 functions for a query that runs in 3 ms, on 16 and 18. The reader now puts the session's jit setting aside for its own statement and puts it back, in one batch (PgCatalog.WithoutJit). The_readers_statement_is_not_compiled_by_the_server holds it on a session with jit_above_cost = 0, with the statement by itself as the control; with the wrapping taken out the test fails on the JIT: section of the plan. The class's live tests went from about 90 seconds to 7.

After the review (the second commit): the refusals the verifier skipped, the expression rule, the savepoint for every failure, the remark for #29, the tests the review named as missing, and point 4 above.

Tests

  • Kernel: 24 new tests in SchemaVerifierTests, on SQLite with a sample probe written against the public contract, and one more in PublicSurfaceTests. The kernel's 894 tests pass, the integration tests on PostgreSQL 16 and 18 among them.
  • PostgreSQL package: 65 live tests in PgCatalogProbeLiveTests, on PostgreSQL 16.15 and 18.1, four in ColumnByHandIdentityTests, and two in ConsumerCompileTests: a startup check compiles with nothing suppressed, and a class that implements ICatalogProbe is asked for SK9001. The package's 703 tests pass on each server.
  • The cross-tests with CodoMetis.ValueRanges (40) and the second provider's 94 tests on SQLite pass. The counts above are from this branch at its head, with main at dd5db80 merged in.
  • The spikes B, D, E and F and the consumer smoke test passed on the same two servers before the rebase onto test: cross-tests with CodoMetis.ValueRanges, on PostgreSQL 16 and 18 #258's main, and were not run again after it: CI's lanes are their run on this commit.
  • Not run here at all: the second provider's 86 tests on SQL Server and the SQL Server cells of spikes D and E. There is no SQL Server on the machine that built this.

Each rule was taken out in turn, alone, and the tests run against it (49 rules before the review and 13 after it; the PostgreSQL ones on 18). What failed:

Taken out Failed
A contributor without a probe is Unverifiable (made Present) A_contributor_without_a_probe_is_unverifiable_and_the_report_is_not_clean, Without_a_probe_to_ask_the_database_is_not_reached
The check of a probe's answer An_answer_that_is_not_one_finding_for_each_object_is_refused, four of its five rows
The transaction handed to the probe Inside_a_transaction_the_probe_reads_through_it
The connection closed again The_connection_is_closed_again_and_one_that_was_open_stays_open, The_connection_is_closed_when_a_probe_throws
The design-time model (the runtime model read) A_model_no_migration_could_be_made_of_is_refused_as_the_planner_refuses_it. Beside_a_runtime_model_the_design_time_model_is_read passes either way: it is a record
IsClean counts Unverifiable as clean A_report_is_clean_only_when_every_object_is_present and two more
The refusal without UseSchemaKit() A_context_without_UseSchemaKit_is_refused
A Different needs a facet, an Unverifiable a reason A_difference_names_a_facet_and_an_unverifiable_finding_its_reason, each
The table, the kind (an index's, a constraint's with and without an index), the access method One row each of An_index_altered_by_hand_… or A_constraint_altered_by_hand_is_different_with_the_facet_named
A key's column, its order, where its nulls stand, the number of keys, a stated operator class, a stated collation, an element's operator One to three rows each of the same two tests
An operator class where none is stated The_default_operator_class_of_a_varchar_is_texts
A collation where none is stated The_collation_of_a_key_that_states_none_is_its_columns
Uniqueness, NULLS NOT DISTINCT, included columns, storage parameters, a parameter the model does not state, a constraint's included columns and parameters, deferral One to four rows each
A flag compared by what it means The_same_index_written_otherwise_is_present, the deduplicate_items=false row
Whether there is a condition A_condition_that_is_gone_is_different and two rows
Validity An_index_a_failed_concurrent_build_left_is_not_present
An expression, a condition that was not compared makes Unverifiable An_object_with_an_expression_or_a_condition_is_unverifiable_and_never_present, A_constraint_with_a_condition_is_unverifiable_and_never_present
The refusal of the catalog caught; rolled back to the savepoint A_role_that_may_not_read_the_catalog_is_unverifiable_for_every_object, each
The table's schema asked; a table without one found on the session's path; the index looked for in its table's schema An_index_is_looked_for_in_the_schema_of_its_table, A_table_without_a_schema_is_the_one_the_session_finds
The pieces of the default-operator-class rule: a domain's base type, a type read as another without a conversion, the exact class first, arrays and enums and ranges, the preferred type Whether_an_operator_class_is_the_default_is_the_servers_word, each; the exact class first fails 42 of 54
The order bits of indoption the other way round Three tests
After the review: the verifier asks the planner's refusals A_model_that_takes_a_name_twice_is_refused_as_the_planner_refuses_it, A_model_that_needs_what_it_does_not_declare_is_refused_as_the_planner_refuses_it, and on PostgreSQL A_declaration_under_the_name_of_an_index_of_EF_Cores_own_is_refused_and_not_found_in_it; the names alone and the dependencies alone fail the first and the second
A declared expression left open where the catalog has a column (made a difference again) No_index_of_the_5x_fixture_is_different_or_missing, both rows, and A_column_where_an_expression_is_declared_is_not_taken_for_a_difference
Every failure undone to the savepoint (only the refusal) A_read_that_fails_inside_a_transaction_leaves_the_callers_transaction_usable
'p' among the tables' kinds; 'I' among the indexes' An_index_on_a_partitioned_table_is_found, each
named.conrelid = t.oid A_constraint_of_the_name_on_another_schemas_table_is_not_the_declared_one and two more
A parameter's value lower-cased; a number by its value A_storage_parameter_that_is_a_word_is_compared_by_what_it_says and the deduplicate_items='OFF' row; the fillfactor = '070' row
The table's schema asked (again, for the new test) A_models_default_schema_is_where_its_tables_are_looked_for, An_index_is_looked_for_in_the_schema_of_its_table
A key compared without its parentheses (put back) Three of ColumnByHandIdentityTests' four

One clause is held by no test: in the default-operator-class rule, that only an implicit cast counts. Taken out, nothing fails, and with regclass and regtype added to the types nothing fails either: an index under a class reached by a cast that is not implicit is one the server refuses to make, so no index can show the difference. It is in the query because it is in the server's rule. The test's remarks and CLAUDE.md say so.

Four mutations did not compile at first (a local function left unused) and were redone by switching the condition off; their rows above are from the second run.

🤖 Generated with Claude Code

…f its own

context.Database.VerifySchemaKitAsync() reads the database's catalog for
every object the model declares and reports each as Present, Missing,
Different with what differs, or Unverifiable with the reason. It reads the
database and not the migrations history, so it does not depend on which
differ scaffolded a migration.

Kernel: the report a consumer reads, unmarked; ICatalogProbe in the
contract; the planner's reading of a model moved, unchanged, into
DeclaredObjects, which the verifier shares.

PostgreSQL: probes for PgIndex and PgExclusion over pg_index, pg_class and
pg_constraint. An expression key and a condition are not compared here: an
object that has one is Unverifiable, never Present (#259).

Closes #256

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@CaffeinatedCoder CaffeinatedCoder added this to the 11.0.0 milestone Nov 28, 2026
@CaffeinatedCoder CaffeinatedCoder added enhancement New feature or request package/core Affects CodoMetis.EFCore.SchemaKit (the kernel) package/postgresql Affects CodoMetis.EFCore.SchemaKit.PostgreSQL area/migrations The differ and generator decorators, snapshot format, EnsureCreated and the UseSchemaKit wiring labels Nov 28, 2026
@CaffeinatedCoder

Copy link
Copy Markdown
Owner Author

Review at c3acb44, with an independent second reading. Not merged yet: the checks are green and five changes are asked for.

Found sound: every facet a PgIndex or PgExclusion descriptor holds is compared with its catalog column, and an expression or a condition never yields Present; the default-operator-class query follows the server's rule clause by clause; the planner's code moved into DeclaredObjects is main's, renamed; SK9001 is on ICatalogProbe and CatalogProbeContext alone.

Asked for:

  1. The verifier skips the planner's model-level refusals. SchemaVerifier calls DeclaredObjects.Of alone; NameClaims.Require and RequireDependencies stayed in Plan. A model migrations add refuses with SK1001 is verified: with HasIndex(x => x.Code) and pg.BTree(x => x.Code) under one name, a database migrated without SchemaKit holds EF Core's index of that name and the declared one is Present. Reproduced on SQLite with the sample probe: two declarations of one name, EnsureCreated() throws SK1001, VerifySchemaKitAsync() returns a report.
  2. A key of SQL written by hand that is one column is Different on a database SchemaKit built, as the 5.x fixture's ux_account_kind_title has it: PostgreSQL keeps ("Kind") as a column. The correction is in Catalog verifier: expression keys and conditions, compared through a shadow #259's branch and moves into this one, with a test over the fixture's Initial.sql and over EnsureCreated() that no object is Different.
  3. A failure other than 42501 inside the caller's transaction leaves it aborted: the savepoint is rolled back in that one catch alone. A cancelled token or a statement timeout then costs the caller its transaction.
  4. Descriptor equality moved, and nothing records it: a column key and hand-written SQL of the same text are one object now, where main writes a drop and a create. It matches the catalog. To be confirmed by the maintainer, held by a test, and said in the changelog.
  5. Rows the live suite lacks: a partitioned table; a constraint of the declared name on the same table in another schema; a storage parameter stored in another form, and buffering.

And one sentence on ICatalogProbe for #29: a catalog that leaves out what a role may not see, instead of refusing, has to be told from a missing object by the probe.

CaffeinatedCoder and others added 2 commits November 28, 2026 23:45
The verifier read the consumer's own model and skipped the planner's two
refusals for it: a name taken twice (SK1001) and an object that needs one
the model does not declare. A declared index under the name of one of EF
Core's own was found in that index, on a database EF Core had built
alone, and the report was clean. Both refusals now stand beside the
model's reading (DeclaredObjects.RequireSound) and both readers ask them.

A declared expression is not judged against the catalog's columns: SQL
written by hand that is one column in parentheses is a column there, and
5.x's fixture has such an index, which was reported as different.

Every failure of the read inside a transaction of the caller's is undone
to the savepoint, not the refusal of the catalog alone.

Tests for what the reader's query was not asked: a partitioned table, a
constraint of the name on another schema's table, a storage parameter's
case and number form, a model's default schema. And for what makes a
column key and SQL by hand that is that column one object to the planner.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
CaffeinatedCoder added a commit that referenced this pull request Nov 28, 2026
With what the review of #265 changed. The shadow decides what the first
slice leaves open: a column where an expression is declared. Its own
first read, of the tables' columns, is in a savepoint too.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The planner prices the reader's query far above jit_above_cost, and on a
server as it is installed PostgreSQL compiled some 260 functions for a
query that runs in three milliseconds: 0.7 seconds for every call of the
verifier, on 16 and 18. The reader puts the session's setting aside for
its own statement and puts it back after, in one batch.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@CaffeinatedCoder

Copy link
Copy Markdown
Owner Author

Second review, at 3204f02. The six points of the first review are answered, and I found nothing that blocks.

Checked beyond reading the diff, in a scratch copy of the commit: with objects.RequireSound(declared, model) taken out of SchemaVerifier.VerifyAsync, A_model_that_takes_a_name_twice_is_refused_as_the_planner_refuses_it and A_model_that_needs_what_it_does_not_declare_is_refused_as_the_planner_refuses_it both fail with "no exception was thrown". With the line back, the 24 tests of SchemaVerifierTests pass.

Two notes that do not block, for #259, which touches the same code:

  1. PgCatalogProbe.ReadAsync: when the rollback to the savepoint throws itself (a connection that a cancelled command left broken), its exception replaces the one that failed the read. A caller that cancelled then catches something other than OperationCanceledException. The first exception should be the one that leaves the method.
  2. The_readers_statement_is_not_compiled_by_the_server also asserts that the fastest of three readings takes under 250 ms. The EXPLAIN half holds the rule by itself; the timing can fail on a loaded runner for a reason that is not the rule.

Still open: the four points under "Decided here" are the maintainer's to confirm. The merge waits for that.

@CaffeinatedCoder

Copy link
Copy Markdown
Owner Author

The maintainer confirmed the four points under "Decided here" on 2026-11-29, as written. Merging at 3204f02.

@CaffeinatedCoder
CaffeinatedCoder merged commit 903c219 into main Nov 29, 2026
11 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the feat/256-catalog-verifier branch November 29, 2026 06:49
CaffeinatedCoder added a commit that referenced this pull request Nov 29, 2026
#265 gave preview.3's changelog a "### Changed" heading, as this branch
does, and git merged the two without a conflict into two headings one after
the other. One heading is kept, with #256's entry first and #248's second.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/migrations The differ and generator decorators, snapshot format, EnsureCreated and the UseSchemaKit wiring enhancement New feature or request package/core Affects CodoMetis.EFCore.SchemaKit (the kernel) package/postgresql Affects CodoMetis.EFCore.SchemaKit.PostgreSQL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Catalog verifier: the kernel's part, existence and what the catalog states structurally

1 participant