fix(pg-topo): break inline foreign-key cycles instead of dropping statements#314
Open
avallete wants to merge 2 commits into
Open
fix(pg-topo): break inline foreign-key cycles instead of dropping statements#314avallete wants to merge 2 commits into
avallete wants to merge 2 commits into
Conversation
Reproduces #311: a mutual foreign-key cycle between two tables makes analyzeAndSort drop the cycle participants and every dependent statement from `ordered`, leaving only a CYCLE_DETECTED diagnostic. RED against current code: ordered.length: 1 (expected 5+2) CYCLE_DETECTED: 1 (expected 0) cycleGroups: 1 (expected 0) ALTER_TABLE count: 0 (expected 2) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DSsCzpn9AZC52rUj8tmjHz
…tements A mutual FK reference between tables is a genuine dependency cycle that Kahn's algorithm cannot order, so the previous code dropped the cycle participants — and every statement downstream of them — from `ordered`, keeping only a CYCLE_DETECTED diagnostic. The declarative apply engine then built an incomplete shadow schema from the truncated statement list, which pg-delta diffed into spurious DROP statements for objects that still existed (#311). Fix, mirroring pg_dump: - New src/rewrite/break-fk-cycles.ts detects FK cycles among CREATE TABLE statements and defers each cross-cycle FK into a standalone ALTER TABLE ... ADD CONSTRAINT (moving the constraint AST verbatim, so the name, columns, and ON DELETE/ON UPDATE actions are preserved). Column-level FKs gain an explicit fk_attrs naming the owning column. Self-referential FKs stay inline. - analyzeAndSort runs the breaker when a cycle is present and re-derives the graph from the rewritten statement set. - As a safety net, any statement still left in an unbreakable cycle is now appended to `ordered` in deterministic order rather than dropped, so consumers always receive every statement. GREEN: the regression test's issue schema now yields ordered.length 7 (5 originals + 2 deferred ALTERs), cycleGroups 0, CYCLE_DETECTED 0, ALTER_TABLE 2. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DSsCzpn9AZC52rUj8tmjHz
🦋 Changeset detectedLatest commit: cb8406a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
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
Fixes #311. A mutual foreign-key reference between tables is a genuine dependency cycle that Kahn's algorithm cannot order, so
analyzeAndSortpreviously dropped the cycle participants — and every statement downstream of them — from theorderedresult, leaving only aCYCLE_DETECTEDdiagnostic. The declarative apply engine then built an incomplete shadow schema from the truncated list, which pg-delta diffed into spuriousDROPstatements for objects that still existed.The fix mirrors
pg_dump: when a cycle is detected, cross-cycle FK constraints are deferred into standaloneALTER TABLE ... ADD CONSTRAINTstatements so the statement set becomes acyclic and applies cleanly.Key Changes
New module
src/rewrite/break-fk-cycles.ts:CREATE TABLEstatements participating in each cycle.CREATE TABLE).ALTER TABLE ... ADD CONSTRAINT, moving the constraint AST verbatim so the name, columns, andON DELETE/ON UPDATEactions are preserved.fk_attrsbecause the column is implicit) gain an explicitfk_attrsnaming the owning column.Updated
src/analyze-and-sort.ts:buildStatementNode/buildStatementNodesfor reuse after rewriting.orderedin deterministic order rather than being dropped, so consumers always receive every statement plus theCYCLE_DETECTEDdiagnostic.Tests + changeset:
test/cycle-fk-split.test.ts: statement preservation, cycle resolution, cross-table FK deferral vs. inline self-FK, and ordering.test/cycle-fk-split.validation.test.ts: Docker-backed PostgreSQL validation that the rewritten order applies cleanly.@supabase/pg-topopatch).Notes for reviewers
statementIndex, so the declarative-apply per-input file-path remap keeps working.pg-delta@1.0.0-alpha.20; shipping this to users needs a pg-topo release plus a pg-delta/CLI bump (downstream follow-up).🤖 Generated with Claude Code
https://claude.ai/code/session_01DSsCzpn9AZC52rUj8tmjHz