fix: commit enum ADD VALUE before statements that use the new label (#600) - #618
Conversation
…600) ALTER TYPE ... ADD VALUE was batched into the same transaction group as later statements, so a plan that added a label and used it (e.g. as a column default) failed on apply with SQLSTATE 55P04. Mark ADD VALUE statements as requiring a commit afterwards and close the execution group right after them. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Create-phase objects can still reference a new enum label before its addition is emitted.
Get a fresh assessment by requesting another Copilot review.
Review effort: Balanced
Findings: 1
What changed in this PR
Adds a commit boundary after PostgreSQL enum values are added so later statements can safely use them.
Changes:
- Marks enum
ADD VALUEstatements as requiring a commit. - Splits execution groups at the new boundary.
- Extends the enum fixture with a dependent column default.
| File | Description |
|---|---|
| testdata/diff/create_type/add_type/plan.txt | Updates human-readable plan output. |
| testdata/diff/create_type/add_type/plan.sql | Adds the default alteration SQL. |
| testdata/diff/create_type/add_type/plan.json | Expects two execution groups. |
| testdata/diff/create_type/add_type/old.sql | Adds the existing test table. |
| testdata/diff/create_type/add_type/new.sql | Uses the new enum label as a default. |
| testdata/diff/create_type/add_type/diff.sql | Updates expected migration SQL. |
| internal/plan/plan.go | Closes groups after commit-required statements. |
| internal/diff/type.go | Marks enum additions as commit-required. |
| internal/diff/diff.go | Adds statement commit-boundary metadata. |
| internal/diff/collector.go | Propagates commit-boundary metadata. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
A newly created object (e.g. a table with a column default) may use a label being added to an existing enum. Emit enum label additions ahead of the create phase so they run, and commit, before any consumer. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The current ordering can commit preceding destructive drops if a later migration group fails.
Get a fresh assessment by requesting another Copilot review.
Review effort: Balanced
Findings: 1
Resolved since last review (1)
ADD VALUE forces a commit right after it, so drops emitted ahead of it were committed too and could not roll back if a later group failed. Emit enum label additions first so the unavoidable partial commit is limited to the additive enum change. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Apply must retain one database session across execution groups so session-scoped settings remain effective.
Get a fresh assessment by requesting another Copilot review.
Review effort: Balanced
Findings: 1

Summary
Adding an enum label and using it in the same plan (e.g. as a column default) produced a plan that failed on apply:
PostgreSQL requires
ALTER TYPE ... ADD VALUEto be committed before the new label can be used, butgroupDiffsbatched every non-rewrite statement into one transaction group.Fix:
SQLStatement.RequiresCommitAfter(not serialized) and set it for enumADD VALUEstatements.groupDiffscloses the current execution group right after such a statement.ADD VALUEstill shares a transaction with the steps before it; everything after it starts a new group.The plan now shows the boundary explicitly:
Atomicity note: if a later group fails, the added label stays. That is harmless — re-planning sees the label as present and emits only the remaining steps.
Fixes #600
Test plan
Folded the scenario into
testdata/diff/create_type/add_type(the existing enumADD VALUEcase): a table whose default switches to the newly added label. Failed with 55P04 before the fix, passes after.🤖 Generated with Claude Code