Skip to content

fix(pg-delta): drop out of order operations#192

Merged
avallete merged 3 commits intomainfrom
fix/drop-out-of-order
Apr 15, 2026
Merged

fix(pg-delta): drop out of order operations#192
avallete merged 3 commits intomainfrom
fix/drop-out-of-order

Conversation

@avallete
Copy link
Copy Markdown
Member

@avallete avallete commented Apr 14, 2026

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:

  1. FK-related table drops were ordered as:

    • DROP TABLE challenge_levels
    • DROP TABLE user_challenge_progress

    That fails because the FK on user_challenge_progress still depends on challenge_levels.

  2. Publication membership changes were ordered as:

    • DROP TABLE challenge_levels
    • ALTER PUBLICATION pub DROP TABLE challenge_levels

    That 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:

  • FK dependencies exist at the constraint level
  • publication-to-table dependencies already exist in the dependency data

The actual gap was in the change metadata that feeds the sorter:

  • DropTable only exposed table and column stable IDs, so FK edges that only existed at the constraint level were not influencing whole-table drop ordering.
  • AlterPublicationDropTables exposed requires, but not drops, so it was treated like a non-destructive ALTER and 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:

  • DropTable now includes table constraint stable IDs in its drop-phase metadata, which lets FK constraint dependencies affect whole-table drop ordering.
  • AlterPublicationDropTables now exposes dropped table stable IDs, so it participates in the destructive/drop phase and runs before DROP 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.ts
  • PGDELTA_TEST_POSTGRES_VERSIONS=17 bun run test tests/integration/publication-operations.test.ts
  • PGDELTA_TEST_POSTGRES_VERSIONS=17 bun run test tests/integration/trigger-operations.test.ts
  • bun run test src/core/objects/publication/changes/publication.alter.test.ts
  • bun run test src/core/objects/table/changes/table.drop.test.ts

So 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

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 14, 2026

🦋 Changeset detected

Latest commit: c9b4f6f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@supabase/pg-delta Patch

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 14, 2026

Open in StackBlitz

npm i https://pkg.pr.new/supabase/pg-toolbelt/@supabase/pg-delta@192
npm i https://pkg.pr.new/supabase/pg-toolbelt/@supabase/pg-topo@192

commit: c9b4f6f

Copy link
Copy Markdown
Collaborator

@jgoux jgoux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work on this!

@avallete avallete added this pull request to the merge queue Apr 15, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to a conflict with the base branch Apr 15, 2026
@avallete avallete enabled auto-merge April 15, 2026 08:09
@avallete avallete added this pull request to the merge queue Apr 15, 2026
Merged via the queue into main with commit d15eb48 Apr 15, 2026
58 checks passed
@avallete avallete deleted the fix/drop-out-of-order branch April 15, 2026 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Drop out of order with FK constraints

2 participants