Repository navigation
Description
2026-11-11: the maintainer placed this in the
11.0.0milestone.design/schemakit-core-plan.mdsection 3.12 still says the row guard comes after 11.0.0 ("11.0.0 creates no objects on SQLite. SQLite enters with the row guard"); that sentence has to follow this decision, see "Open decisions" below.
Problem
A consuming application had to hand-write CREATE TRIGGER in a migration to stop rows in a final state from being deleted. Nothing in the model records that trigger, nothing diffs it, and EnsureCreated() never creates it: the test database accepts a delete the production database refuses.
SchemaKit has no issue for this kind. design/schemakit-api.md holds it as one row of the section 15 table (a kernel union RowGuard, owner RowConstraint) and points to the detailed design, which sits in an unmerged PR of the 5.x repository: design/delete-guard.md (CaffeinatedCoder/EFCore.ComplexIndexes#65, issue CaffeinatedCoder/EFCore.ComplexIndexes#64). That document was written for "5.x first, then a transfer". 5.x never shipped a guard and takes fixes only, so the guard ships first here, and its sections 3, 7, 9 and 10 (the 5.x declaration, the v1 blob CustomRowConstraint:Constraints, the 5.6.0 placement) no longer apply. No 5.x snapshot contains a guard, so SchemaKit needs no v1 reader row for it.
Change
A row guard is a declared trigger that raises when a condition on the affected row holds. On PostgreSQL, from the design's sections 6 and 8:
- One raise-only function,
codometis_row_constraint() RETURNS trigger LANGUAGE plpgsql, in the guarded table's schema. It raisescheck_violation(23514) withCONSTRAINT = TG_NAME, the table and the schema, and the message fromTG_ARGV[0]or a default built fromTG_OP. Its name, signature and body are permanent from the first release that renders them. - One trigger per guard,
BEFORE DELETE … FOR EACH ROW [WHEN (…)] EXECUTE FUNCTION …, named by the declaration. The application dispatches on(SqlState, ConstraintName), the code path it has for CHECK constraints. - A kernel union
RowGuardwith the provider markers on its cases (OnDelete(when?),OnUpdate(when?),OnTruncate,Immutable(paths)), per-provider factories, and a requirednamewherever a predicate is given. - Name claims:
Trigger(table, name)andConstraint(table, name).
What was verified on PostgreSQL 14 and 18 (design section 11) and must hold in the implementation:
| Finding | Consequence |
|---|---|
ALTER TRIGGER … RENAME keeps the trigger's arguments |
The function reads the name from TG_NAME, never from an argument |
CREATE OR REPLACE FUNCTION by a second migration role fails with 42501, and would let a later package version change every existing guard |
Create the function if missing; the body is frozen; a change is a new function name |
ALTER COLUMN … TYPE on a column in a trigger's WHEN fails at apply time; DROP COLUMN is refused |
ImpactOf answers a forced rebuild for a guard that reads the column (#24, measured in #20) |
WHEN (OLD."Status" <> 'Draft') lets a row with a null status be deleted |
Guard predicates carry C# null semantics (#26) |
| A trigger name over 63 bytes is truncated with only a NOTICE | The dialect's identifier check refuses it at the declaration |
| A CHECK constraint may take a guard's name on the same table | The Constraint claim reports the collision (#44) |
TRUNCATE … CASCADE deletes guarded rows; session_replication_role = replica lets a guarded delete through |
Documented limits; the first is what OnTruncate is for |
| Cascades from a parent raise with the child's table and guard name | Covered by a behaviour test |
It builds on the contributor contract and the declaration store (#45, #7), change planning and impact (#24), the resolver (#43), the lambda translator (#26) and validation (#44). Spikes D (#19) and E (#20) already cover a trigger in idempotent scripts and its impact answers.
Open decisions
- Scope in 11.0.0. The delete guard on PostgreSQL is the consumer case. Whether
OnUpdate,OnTruncateandImmutable(which needs a two-root predicate,OLDandNEW) ship with it or follow. - SQLite and SQL Server. The reason for the kernel union is that a model on PostgreSQL in production and SQLite in tests declares the guard once. Plan section 3.12 says 11.0.0 creates no objects on SQLite. Either the SQLite renderer enters 11.0.0 with the guard, or the guard is PostgreSQL-only in 11.0.0 and a declaration is refused or ignored on SQLite under the rule of A declaration for another provider is an error, unless the options ignore that provider #22. SQL Server renders
AFTER DELETEwithTHROWand has no truncate guard; it carries the name in the message text only. - Names that land in snapshots and databases for good: the owner
RowConstraint, the function namecodometis_row_constraint, the default trigger nameRC_{table}, the factory name (pg.DeleteGuard). - Which preview slice of the epic it belongs to (SchemaKit 11.0.0-preview.1: the first slice of the new API, on EF Core 11 / Npgsql 11 under the new ids #2 is preview.1).
Done when
- A declared delete guard scaffolds, applies and is enforced on PostgreSQL 16 and 18: a delete of a guarded row fails with 23514 and the guard's name through
SaveChanges,ExecuteDeleteand a cascade, andEnsureCreated()creates it. - Each row of the findings table has a test that fails without what it guards, on a live server where the finding is about the server (Oracles on live servers: catalog equivalence and behaviour #32).
- The catalog verifier (Catalog verifier: VerifySchemaKitAsync for the PostgreSQL kinds #30) knows the kind, and the golden corpus (Golden corpus per renderer epoch, with snapshot fixtures #33) holds its DDL.
- The design lives in this repository's
design/, rewritten for SchemaKit alone, and plan section 3.12 says what was decided under 2.
Activity
CaffeinatedCoder commentedon Nov 16, 2026
Measured in spike D (#19,
spikes/D.ScriptsAndBundles, 2026-11-16), for the guard's trigger and function:CREATE FUNCTIONwith a body of several lines andCREATE TRIGGER, as twoSqlOperations, apply frommigrations script, frommigrations script --idempotentapplied twice and from a bundle run twice. The trigger refuses the row each time, and the second run changes nothing.IF, the body's lines included, and PostgreSQL keeps the body as sent:prosrcis 153 characters after the plain script and after the bundle, 181 after the idempotent script. The trigger behaves the same. A comparison of the function's text between two databases built on different paths (the catalog oracle of Oracles on live servers: catalog equivalence and behaviour #32, the verifier of Catalog verifier: VerifySchemaKitAsync for the PostgreSQL kinds #30) does not come out equal.CREATE TRIGGERwritten as a statement fails in both scripts and applies only from a bundle. InsideEXEC(N'…')it survives every path; what the contract lacks for that is A trigger on SQL Server has to be written inside EXEC, and the contract cannot write a statement with line breaks into a string constant #156.