Skip to content

feat: the PostgreSQL package, its dialect, and exclusion constraints from the declaration to the database - #77

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
feat/pg-exclusion-package
Nov 12, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
feat/pg-exclusion-package

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #76

Problem

No package had a real contributor: the kernel could store, resolve and plan a declaration, and nothing could be declared with it. #25 is the first feature, and this is its first slice.

Change

Kernel

  • KeyTarget (Column | Template) and Predicate, public, built from a lambda by KeyTarget.Of and Predicate.Of<T>: the translator's only public entry points. Predicate.And keeps 5.x's rule for a convention that runs twice.
  • ValueList<T> loses its experimental mark. PublicSurfaceTests has a third list, the declaration values, and holds that none of them is marked.

src/CodoMetis.EFCore.SchemaKit.PostgreSQL, written against the kernel's public contract alone (no InternalsVisibleTo)

  • UseNpgsql(cs, o => o.UseSchemaKit()), and AddEntityFrameworkSchemaKitNpgsql() for a caller-owned provider.
  • NpgsqlSchemaDialect counts an identifier's bytes (from feat: the SQL vocabulary, statements with no hole for a string #42). Registered with Replace, so it holds in either order of the two extensions.
  • PgExclusion (Gist | SpGist), ExclusionElement, ExclusionOperator, Deferral; PgSchema<T> with Equal, Overlaps (range, nullable range, array, list), WithUncheckedOperator, Exclusion(…, where:, deferral:, name:); DeclarePostgres; PgSchema.For<T>(); AndWhere.
  • The contributor: ALTER TABLE … ADD CONSTRAINT … EXCLUDE USING …, DROP CONSTRAINT without IF EXISTS, a rename in place and a rebuild for anything else, ImpactOf as the plan's section 3.7 measured it.

CI: both lanes run both test projects, and each project has to have run a test.

Decisions to confirm

None is expensive to change before preview.1; all are permanent after 11.0.0.

  1. The stored form of Pg.Exclusion: kinds gist and spgist; elements (or element), each with column or sql and op; where as a string; deferral as notDeferrable | initiallyImmediate | initiallyDeferred.
  2. KeyTarget, Predicate and ValueList<T> are not experimental. A consumer writes and reads them.
  3. The package's namespace is CodoMetis.EFCore.SchemaKit.PostgreSQL, extension methods included, so a consumer needs that using beside the kernel's.
  4. AddEntityFrameworkSchemaKitNpgsql() as the name of the caller-owned-provider registration.
  5. WithUncheckedOperator refuses =, and SpGist refuses an equality element, as Gist does.
  6. A dropped column that SQL without a placeholder may read is answered RebuildAround: right whether the constraint read it or not.

Found on the way

  • Npgsql's helper writes a lower-case name bare, so the statements read ALTER TABLE role_grants, not "role_grants".
  • A constraint name that is taken fails with 42P07 (its index is a relation), not 42710.
  • A record's with goes through init and not the constructor: the first version of the new values let with { Template = "" } through. Every one checks in init now.

Tests

413 kernel tests, 40 PostgreSQL tests without a server, and 18 on a live one, green on PostgreSQL 16.15 and 18.6 locally. 125 probes unchanged.

  • Stored form: two keys and two values verbatim; the keys were recomputed outside the code from the documented rule.
  • Statements: each diff's operations in their order, SchemaKit's as SQL and EF Core's by type. A model against its compiled snapshot differs in nothing, in both directions.
  • Live, a database per test: EnsureCreated(), the applied create script and a first migration each leave the same catalog entry. Overlapping rows are refused (23P01) and rows that differ in grantee, role, period or revocation are not. A deferred constraint passes inside a transaction and fails at commit. Eleven model pairs are migrated and compared with what EnsureCreated() builds for the target, by pg_get_constraintdef. A rename keeps the constraint's OID. A taken name fails and replaces nothing. A drop leaves the namesake in another schema.

Each guard shown to fail, in a scratch copy, 22 mutations, none survived:

Mutation Fails
Dialect counts characters The_dialect_counts_a_names_bytes, A_constraint_name_the_server_would_shorten_…
Dialect added with TryAdd those two, The_kernels_call_before_or_after_changes_nothing, A_caller_owned_provider_…
Contributor not registered 19 tests
Drop names the table without its schema A_drop_names_the_table_with_its_schema; live Dropping_a_constraint_leaves_its_namesake_in_another_schema
A dropped column is always Lost A_dropped_condition_column_…; live Dropping_a_column_only_the_condition_reads
A dropped column is always RebuildAround A_dropped_element_column_…
A renamed column, or table, is Unaffected the three rename tests; the table one
Plan always rebuilds Another_name_is_a_rename_in_place, the default-name rename; live Renaming_a_constraint_keeps_the_constraint
The condition's reads are no dependency A_renamed_column_of_the_condition_alone_…
Expression element without parentheses A_constraint_without_a_name_gets_the_default_one
Deferral rendered the other way 4 offline, 10 live, A_deferred_constraint_is_checked_at_commit among them
Default name from rendered SQL A_renamed_column_renames_a_constraint_that_is_named_after_it
Equality-only elements accepted; WithUncheckedOperator takes = No_way_around_the_factory_…; An_unchecked_operator_…
where not written; deferral read as its default; an element with both targets read as a column the stored-form and read-model tests; the refusal rows
Every lambda a template; with bypasses the empty-text rule; And always appends; ValueList marked again DeclarationValueTests, PublicSurfaceTests

Not covered here: PostgreSQL 16 in CI (#5), SQL Server, the compile probes against the shipped assemblies.

🤖 Generated with Claude Code

…from the declaration to the database

Closes #76.

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/constraints Exclusion constraints, row guards and other constraint-like objects 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.

PgExclusion, first slice: the PostgreSQL package, its dialect, the declaration and its contributor

1 participant