feat(ddl): PostgreSQL EXCLUDE constraints with range-overlap support - #160
Merged
Conversation
Generalises UNIQUE — each element pairs a column/expression with a commutative operator (e.g. `room WITH =, during WITH &&`). Refused on MySQL / SQLite / MSSQL via the EXCLUDE_CONSTRAINTS feature flag. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
EXCLUDEtable-level constraint — a generalisation ofUNIQUEwhere each element pairs a column/expression with a commutative SQL operator (room WITH =, during WITH &&). The flagship use case is range-overlap exclusion for booking systems, replacing race-condition-prone application code with a single DB-enforced invariant.WHEREpredicate (partial exclude — "at most one active row per priority") and theUSING <method>clause (defaults togist).EXCLUDE_CONSTRAINTSfeature flag; MySQL / SQLite / MSSQL throwUnsupportedDialectFeatureErrorat print time.Files
src/ast/ddl-nodes.ts— newExcludeConstraintNode+ExcludeElement, added to theTableConstraintNodeunion.src/schema/table.ts— newExcludeDef+ExcludeElementDeftypes,excludesfield onTableConstraints,normalizeExcludeDefhelper.src/migrate/diff.ts—materializeExcludelowersExcludeDef→ AST;signConstraintfolds method + elements + WHERE into the signature so any body change surfaces as drop+add.src/printer/ddl.ts— emitsEXCLUDE [USING <method>] (<expr> WITH <op>, …) [WHERE (<pred>)]on PG; refuses on every non-PG dialect viaassertFeature("EXCLUDE_CONSTRAINTS").src/dialect/features.ts— newEXCLUDE_CONSTRAINTSfeature (PG only).src/utils/security.ts— newvalidateOperatorhelper (1-4 ASCII punctuation char whitelist) — operator tokens are spliced verbatim into emitted DDL, so they get the same validation as function names and data types.src/schema/index.ts— re-exportsExcludeDef/ExcludeElementDef.test/migrate/exclude-constraints.test.ts— 18 new tests: schema-DSL preservation, diff materialization, drop+add on method/elements/WHERE change, four dialect-refusal cases, security validation of unsafe operators/method names, and a PGlite roundtrip that provesINSERTof an overlapping range fails with the constraint violation.docs/recipes.md— new "PostgreSQL EXCLUDE constraints" section with the booking-system pattern, partial-exclude variant, and dialect-support matrix.Test plan
pnpm fmt && pnpm lint && pnpm typecheck && pnpm vitest runall greenorigin/main(e4f6a4c— Agent B's dateAdd / dateSub landed clean)audit*-regressions.test.tsmodificationsTradeoffs
during WITH &&) rather than the composite(room WITH =, during WITH &&)because PGlite doesn't ship thebtree_gistextension that the composite form needs for the equality element. The composite form is still covered end-to-end via the DDL printer test (which asserts the exact emitted SQL); the integration test then proves the constraint is actually enforced at INSERT time.🤖 Generated with Claude Code