Skip to content

feat: the contributor contract and the declaration store, where a declaration never replaces another - #46

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
feat/45-contract-and-store
Nov 11, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
feat/45-contract-and-store

Conversation

@CaffeinatedCoder

@CaffeinatedCoder CaffeinatedCoder commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

Closes #45

First slice of #7 and #9, which stay open; each has a dated note saying what is left.

Problem

The host splices what a planner returns, and the only planner is the tests' stand-in. There was no type a declaration could be, nowhere to store one, and no contract a contributor could implement. A consumer could declare nothing.

The store is also where the draft's confirmed defect lived (plan, section 3.4): its identity was Method|Targets|Where and Declare called SetAnnotation, so a unique index followed by a plain one over the same column wrote one key twice. The unique index was gone, with no error, and the migration applied clean.

Change

All in src/CodoMetis.EFCore.SchemaKit, public and [Experimental("SK9001")].

Declarations (Declarations/)

  • ISchemaObject and ISchemaDeclaration<TSelf>. The codec is static abstract (OwnerId, Read), so the read model needs no service provider.
  • Declare, Redeclare, GetDeclarations<T>(), FindDeclaration<T>(name) on EF Core's entity types. Declare and Redeclare ask for the wiring stamp first.
  • The key, CodoMetis:{Owner}:{hash}: over kind and name for a named declaration, over kind and every property that is not operational for an unnamed one. The kernel computes it from what the declaration writes; a contributor cannot hash one thing and store another.
  • Declare never replaces. A key that is taken, on the entity type or earlier in the same call, throws before anything is stored. Redeclare is the only replacing path; it compares results only, so two names can be swapped in one call.
  • The value: {"$kind":…,"v":2,"r":…,"name":…,…}, written by hand in a fixed order through SchemaJsonWriter. Nothing is omitted but a null, and SchemaJsonReader has no default: a method either requires its property or returns null. An unknown v, a property nothing read, and a reader that builds anything but the declaration that was stored are refused.

Contract (Contributors/)

  • ISchemaContributor and SchemaContributor<TDeclaration, TDescriptor>, with Plan and ImpactOf abstract. ISchemaDescriptor has Scope, Name, Dependencies and WithName.
  • The closed unions SchemaChange, Impact, BaseChange, Dependency, NameClaim, SchemaPlacement; ValueList<T> and Operational<T>.
  • AddSchemaContributor<T>(), which adds a contributor to a context's services without touching the two EF services. Two contributors for one owner are refused. A descriptor type that is not a record is refused when its contributor is built.
  • SchemaResolver as what Resolve is handed (table, dialect, a fragment builder). It cannot be derived outside the kernel. Turning paths into SQL is SchemaResolver: paths to columns and SQL, identically on both sides of a diff #43.

Decisions taken here, cheap to change until preview.1

  1. The kind in the key is the case of the union ($kind), not the owner. An index and a trigger of one name under one owner are two declarations; whether the name is taken twice is the name claims' to say (Validation and diagnostics: name claims across kinds, every finding at once #44).
  2. SchemaChange.InPlace carries no statements. The plan showed both a payload and a SchemaSql parameter. Plan writes into the SchemaSql, and the base class refuses an answer that disagrees with what was written.
  3. The stamp is not the format version. CodoMetis:SchemaKit = 1 stays a mark of presence; the version is v on each declaration. This reverses what I recommended before writing the store: a per-declaration version lets one snapshot hold two formats while it is rewritten.
  4. Dependency.WholeTable and Dependency.SchemaObject, where the plan said Table and Object: a case named Table clashes with the Table parameter of its sibling Column (CS8866).
  5. ISchemaObject.NameComparison, defaulting to ordinal, for the plan's rule that names compare as the provider compares them.
  6. A fourth registration method, AddSchemaContributor<T>(), experimental on a type that is not.

Not here: the provider markers and DeclarePostgres / DeclareSqlServer (#25, #29), so the probe X3 waits for them; the 5.x readers (#18, #28); model-level declarations.

Tests

Local runs were on SDK 11.0.100-rc.1 in a scratch copy; this machine has no released SDK. CI is the run that counts.

Suite Before After
Kernel tests, without a server 160 246
Probe project 97 125
  • The stored form is held verbatim: one key and one value in DeclarationStoreTests, the key also derived independently from the documented identity string.
  • The round trip that counts goes through a snapshot. SnapshotRoundTripTests scaffolds one with EF Core's design-time services, compiles it with Roslyn, loads it and reads the declarations from its model, on Npgsql, SQL Server and SQLite, with quotes, a backslash and a line break in the values.
  • The omitted-default rule is held for the writer as a whole, by reflection over its methods, so it stays true of one added later; and the reader has a method for every method of the writer.
  • 14 new kernel probes against the shipping assembly: E10.render-a-declaration, X1, X2, X4 (a resolver of one's own), M1 to M3 (Plan and ImpactOf are mandatory, and ImpactOf answers every base change), P8, and six P9 probes, one per closed union.

Each rule was removed in a scratch copy, and the tests that fail were noted. 35 changes, all fail at least one test:

Removed Fails
Declare refusing a key already on the entity type A_declaration_that_is_already_there_is_refused_and_the_first_stays
Declare refusing a repeated key in one call The_same_declaration_twice_in_one_call_…, Two_declarations_that_differ_only_in_an_operational_option_are_one, Names_are_compared_…
Declare being all or nothing The_same_declaration_twice_in_one_call_is_refused_and_nothing_is_stored and two more
The stamp in Declare; in Redeclare Declaring_on_a_model_without_the_stamp_…; Redeclaring_on_a_model_without_the_stamp_…
Redeclare refusing two results on one key Redeclare_that_would_turn_two_declarations_into_one_…
The kind in a named key The_same_name_on_two_kinds_is_two_declarations and both golden keys
Operational options left out of an unnamed key Two_declarations_that_differ_only_in_an_operational_option_are_one, the golden key
The draft's identity (an unnamed key over the kind alone) Two_unnamed_declarations_that_differ_in_an_option_are_both_kept and two more
Case-insensitive names Names_are_compared_as_the_database_compares_them
The writer writing a false; an empty list 19 tests; 8 tests, the snapshot round trip among them
The reader requiring a property A_property_that_was_not_written_is_not_given_a_default
The unread-property check, at the top; in nested objects A_property_the_declaration_does_not_read_is_refused (both)
The format version check A_format_version_this_version_does_not_know_is_refused
The header check after Read A_reader_that_loses_the_name_or_the_epoch_is_refused
The relaxed JSON encoder An_operator_stays_readable_in_the_stored_text, both golden values
Reserved names; a name written twice; owner, kind, name and epoch checks A_declaration_cannot_write_a_property_the_kernel_writes; A_property_cannot_be_written_twice; A_declaration_that_cannot_be_stored_is_refused
InPlace needing a statement; statements needing InPlace; counting only the plan's own A_plan_whose_answer_and_statements_disagree_is_refused, A_plan_is_judged_by_what_it_wrote_itself
The record check on the descriptor A_descriptor_that_is_not_a_record_is_refused_when_the_contributor_is_built
The routing check A_declaration_or_a_descriptor_of_another_contributor_is_refused
One contributor per owner; TryAddEnumerable in the registration Two_contributors_for_one_owner_are_refused; A_registered_contributor_reaches_the_registry_…
The registry kept out of the differ's constructor A_registered_contributor_reaches_the_registry_and_leaves_the_host_as_it_was
Operational<T> ignoring its value; ValueList<T> comparing by elements 2 tests; 12 tests
The resolver's private protected constructor What_the_kernel_hands_out_cannot_be_made_outside_it
The experimental mark on AddSchemaContributor The_contributor_registration_is_marked_experimental
FindDeclaration refusing an ambiguous name The_same_name_on_two_kinds_is_two_declarations
The read model's order by key The_read_model_is_ordered_by_key_whatever_the_order_of_declaration

One change found a gap. The last one survived at first: EF Core keeps annotations sorted, so a test that compared two orders of declaration with each other could not fail. It now compares with the keys' own order.

After the analysis (3f0ae4e)

An analysis of this branch found no blocker, one silent failure and three guards without a test that fails for them. All four are fixed here; the kernel suite on SDK 11.0.100 is at 252.

  • A Read that changes the declaration is refused. A contributor's Read could take a property from the reader and drop it, or put a default in its place: nothing was unread, the header was intact, and a unique declaration came out of the snapshot as a plain one. DeclarationCodec.Read now writes what Read built and requires its JSON to equal what was stored. A stored value that could not have been stored, such as "r":0, is refused the same way.
  • Tests that were missing, each run against the code with its guard removed:
Removed Fails
The comparison of what was stored with what Read built A_reader_that_drops_a_property_it_read_is_refused
The kind in the header check A_reader_that_returns_another_kind_is_refused
Declare refusing a key already on the entity type, for an unnamed declaration An_unnamed_declaration_that_is_already_there_is_refused_and_the_first_stays
An operational property left out of the key, in Strings, Object, Array and OptionalString in turn (only Boolean was covered) Every_writer_method_keeps_an_operational_property_out_of_the_key
The same inside a nested object one test fails; An_operational_property_of_a_nested_object_is_stored_and_stays_out_of_the_key is the only one on that path

Findings left for issues of their own: a direct ISchemaContributor implementation passes the base class's refusals by (#48), the unnamed key depends on the runtime's Unicode tables through the JSON encoder (#49), the wording of the format-version refusal (#50), a kind that is not checked like an owner (#51), and guards still without a failing test (#52).

🤖 Generated with Claude Code

…laration never replaces another

The kernel gains the types a declaration and a contributor are made of, and the store a
declaration lives in. Nothing turns a declaration into DDL yet: the resolver is #43 and the
planner #24.

Declarations: ISchemaObject and ISchemaDeclaration<TSelf> with the codec as static abstract
members; Declare, Redeclare, GetDeclarations<T>() and FindDeclaration<T>() on EF Core's entity
types. One annotation per declaration, CodoMetis:{Owner}:{hash}. The kernel computes the key from
what the declaration writes: kind and name for a named declaration, kind and every property that
is not operational for an unnamed one. Declare refuses a key that is taken, before it stores
anything; Redeclare is the only replacing path.

The stored value is JSON written by hand in a fixed order, with the format version v and the
resolution epoch r. Nothing is omitted but a null and the reader has no default; an unknown
version, a property nothing read and a reader that loses the name or the epoch are refused.

Contract: ISchemaContributor and SchemaContributor<TDeclaration, TDescriptor> with Plan and
ImpactOf as mandatory members, ISchemaDescriptor with Scope, Name and Dependencies, the closed
SchemaChange, Impact, BaseChange, Dependency, NameClaim and SchemaPlacement, ValueList<T>,
Operational<T>, and AddSchemaContributor<T>(). All public and experimental (SK9001).

Closes #45

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 11.0.0 milestone Nov 11, 2026
@CaffeinatedCoder CaffeinatedCoder added enhancement New feature or request package/core Affects CodoMetis.EFCore.SchemaKit (the kernel) area/migrations The differ and generator decorators, snapshot format, EnsureCreated and the UseSchemaKit wiring labels Nov 11, 2026
…guards are held by tests that fail without them

Findings of the analysis of #46, for #45.

A contributor's Read could take a property from the reader and drop it, or
put a default in its place: nothing was unread, the header was intact, and
the declaration that came out of the snapshot was another one. Read now
writes what it built and requires the JSON to equal what was stored. That
also refuses a stored value that could not have been stored, such as "r":0.

Tests that were missing, each shown to fail without its guard:
- every writer method keeps an operational property out of the key (only
  Boolean was covered), and so does a nested object's operational property
- an unnamed declaration that is already on the entity type is refused
- a Read that returns another kind is refused

Co-Authored-By: Claude Fable 5.2 <[email protected]>
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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The contributor contract and the declaration store: ISchemaContributor, Declare, Redeclare, the annotation key and value

1 participant