fix(pg-delta): drop out of order operations#192
Merged
Conversation
🦋 Changeset detectedLatest commit: c9b4f6f 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.
I dug into the reported DROP-ordering regressions and reproduced both cases with focused integration tests before changing sorter behavior.
Context
There were two concrete failures in generated plan SQL:
FK-related table drops were ordered as:
DROP TABLE challenge_levelsDROP TABLE user_challenge_progressThat fails because the FK on
user_challenge_progressstill depends onchallenge_levels.Publication membership changes were ordered as:
DROP TABLE challenge_levelsALTER PUBLICATION pub DROP TABLE challenge_levelsThat fails because the table must still exist when removing it from the publication.
Root Cause
The interesting part is that the catalog dependency extraction was already capturing the important relationships:
The actual gap was in the change metadata that feeds the sorter:
DropTableonly exposed table and column stable IDs, so FK edges that only existed at the constraint level were not influencing whole-table drop ordering.AlterPublicationDropTablesexposedrequires, but notdrops, so it was treated like a non-destructiveALTERand sorted after the real table drop.Fix
This patch fixes the ordering by making the destructive changes advertise the right stable IDs to the sorter:
DropTablenow includes table constraint stable IDs in its drop-phase metadata, which lets FK constraint dependencies affect whole-table drop ordering.AlterPublicationDropTablesnow exposes dropped table stable IDs, so it participates in the destructive/drop phase and runs beforeDROP TABLE.I also added focused integration coverage for both reported scenarios, plus a small unit assertion around the publication change metadata.
Verification
Verified with:
PGDELTA_TEST_POSTGRES_VERSIONS=17 bun run test tests/integration/fk-constraint-ordering.test.tsPGDELTA_TEST_POSTGRES_VERSIONS=17 bun run test tests/integration/publication-operations.test.tsPGDELTA_TEST_POSTGRES_VERSIONS=17 bun run test tests/integration/trigger-operations.test.tsbun run test src/core/objects/publication/changes/publication.alter.test.tsbun run test src/core/objects/table/changes/table.drop.test.tsSo this ends up being less about missing raw dependency extraction and more about making sure destructive change objects surface enough information for the sorter to use the dependency graph correctly.
Fixes #193