Skip to content

RowGuard: delete guards as declared triggers, with one raise-only function #47

Open

Description

@CaffeinatedCoder

2026-11-11: the maintainer placed this in the 11.0.0 milestone. design/schemakit-core-plan.md section 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 raises check_violation (23514) with CONSTRAINT = TG_NAME, the table and the schema, and the message from TG_ARGV[0] or a default built from TG_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 RowGuard with the provider markers on its cases (OnDelete(when?), OnUpdate(when?), OnTruncate, Immutable(paths)), per-provider factories, and a required name wherever a predicate is given.
  • Name claims: Trigger(table, name) and Constraint(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

  1. Scope in 11.0.0. The delete guard on PostgreSQL is the consumer case. Whether OnUpdate, OnTruncate and Immutable (which needs a two-root predicate, OLD and NEW) ship with it or follow.
  2. 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 DELETE with THROW and has no truncate guard; it carries the name in the message text only.
  3. Names that land in snapshots and databases for good: the owner RowConstraint, the function name codometis_row_constraint, the default trigger name RC_{table}, the factory name (pg.DeleteGuard).
  4. 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

Activity

  1. added this to the 11.0.0 milestone on Nov 11, 2026
  2. added
    enhancement
    package/core
    package/postgresql
    on Nov 11, 2026
  3. added
    area/constraints
    area/migrations
    on Nov 11, 2026
  4. CaffeinatedCoder commented on Nov 16, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Measured in spike D (#19, spikes/D.ScriptsAndBundles, 2026-11-16), for the guard's trigger and function:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/constraintsarea/migrationsenhancementpackage/corepackage/postgresql

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions