Skip to content

fix(BA-6915): drop the column default before retyping sessions.result#12912

Draft
jopemachine wants to merge 2 commits into
fix/BA-6913-fresh-install-missing-indexesfrom
fix/BA-6915-sessionresult-downgrade-default
Draft

fix(BA-6915): drop the column default before retyping sessions.result#12912
jopemachine wants to merge 2 commits into
fix/BA-6913-fresh-install-missing-indexesfrom
fix/BA-6915-sessionresult-downgrade-default

Conversation

@jopemachine

@jopemachine jopemachine commented Jul 16, 2026

Copy link
Copy Markdown
Member

📚 Stacked PRs

Merge in order, top-down. Each PR is based on the one above it, so its diff shows only its own layer. The order is fixed by the alembic chain: #12907's migration takes #12910's parent as its down_revision.

Problem

0b1efbb2db84's downgrade recreates the plural sessionresults type and then:

ALTER TABLE sessions ALTER COLUMN result TYPE sessionresults USING result::text::sessionresults

sessions.result carries a default. PostgreSQL cannot cast an existing default across two unrelated enum types, so this aborts before touching a row:

DatatypeMismatchError: default for column "result" cannot be cast automatically to type sessionresults

The migration guards its DDL with a try/except over a list of expected-divergence SQLSTATEs — but 42804 isn't on it, so the error is reraised and the whole downgrade chain stops here.

The correct order already exists in this repo

dec0deba5893 does the same conversion and succeeds, because it handles the default:

_drop_column_default(conn, table, column)
_alter_column_to_sessionresult(conn, table, column)
_set_column_default_undefined_sessionresult(conn, table, column)

0b1efbb2db84 just omits it. This PR gives it the same order, on both the downgrade and the mirror-image ALTER in upgrade().

Reproduction

On a DB built at head by mgr schema oneshot, downgraded with no skipped revisions:

  1. 150 downgrade steps run cleanly.
  2. dec0deba5893's downgrade recreates the singular sessionresult and converts sessions.result to it.
  3. Crossing 0b1efbb2db84, the has_singular and not has_plural branch fires and aborts.

Confirmed by hand that DROP DEFAULTALTER COLUMN TYPESET DEFAULT completes, leaving result as sessionresults with default 'UNDEFINED'::sessionresults.

Verification

With this fix on top of #12881, #12883, #12887, #12907, #12910, a single straight alembic downgrade from head to d86417a6bf7b — below the deepest RBAC backfill — now completes:

exit=0   steps run: 175   (no skipped revisions)
current: d86417a6bf7b

That path previously stopped after 28 steps. Each fix moved it further: 28 → 84 → 150 → 175, target reached.

Found while surveying the downgrade chain for #12881.

🤖 Generated with Claude Code

@github-actions github-actions Bot added size:S 10~30 LoC comp:manager Related to Manager component require:db-migration Automatically set when alembic migrations are added or updated labels Jul 16, 2026
@jopemachine
jopemachine force-pushed the fix/BA-6914-fk-constraint-name-drift branch from db2ee0a to 1c9d392 Compare July 20, 2026 04:24
@jopemachine
jopemachine force-pushed the fix/BA-6915-sessionresult-downgrade-default branch from 9bddfad to 1571ff3 Compare July 20, 2026 04:24
@jopemachine
jopemachine changed the base branch from fix/BA-6914-fk-constraint-name-drift to fix/BA-6913-fresh-install-missing-indexes July 20, 2026 04:24
@jopemachine
jopemachine force-pushed the fix/BA-6913-fresh-install-missing-indexes branch from 04b8764 to f245ba8 Compare July 20, 2026 04:39
@jopemachine
jopemachine force-pushed the fix/BA-6915-sessionresult-downgrade-default branch from 1571ff3 to 6ab9b00 Compare July 20, 2026 04:39
@jopemachine
jopemachine force-pushed the fix/BA-6913-fresh-install-missing-indexes branch from f245ba8 to d6707d9 Compare July 20, 2026 04:42
@jopemachine
jopemachine force-pushed the fix/BA-6915-sessionresult-downgrade-default branch from 6ab9b00 to 296ce72 Compare July 20, 2026 04:42
@jopemachine
jopemachine force-pushed the fix/BA-6913-fresh-install-missing-indexes branch from d6707d9 to 2925811 Compare July 20, 2026 05:20
@jopemachine
jopemachine force-pushed the fix/BA-6915-sessionresult-downgrade-default branch from 296ce72 to 13ad05d Compare July 20, 2026 05:20
@jopemachine
jopemachine force-pushed the fix/BA-6913-fresh-install-missing-indexes branch from 2925811 to 9317cef Compare July 20, 2026 05:21
@jopemachine
jopemachine force-pushed the fix/BA-6915-sessionresult-downgrade-default branch from 13ad05d to eee9ae0 Compare July 20, 2026 05:21
jopemachine and others added 2 commits July 20, 2026 14:25
The downgrade recreates the plural sessionresults type and then retypes
sessions.result into it. PostgreSQL cannot cast an existing column default
across two unrelated enum types, so the ALTER aborts with
DatatypeMismatchError before touching a row. The migration's try/except
tolerates a list of expected divergence SQLSTATEs, and 42804 is not on it,
so the whole downgrade chain stops here.

Drop the default, retype, then restore it -- the order dec0deba5893 already
uses for the same conversion. The upgrade path runs the mirror-image ALTER
with the same omission, so it gets the same treatment.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jopemachine
jopemachine force-pushed the fix/BA-6913-fresh-install-missing-indexes branch from c181663 to e972044 Compare July 20, 2026 05:25
@jopemachine
jopemachine force-pushed the fix/BA-6915-sessionresult-downgrade-default branch from eee9ae0 to 2422038 Compare July 20, 2026 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:manager Related to Manager component require:db-migration Automatically set when alembic migrations are added or updated size:S 10~30 LoC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant