fix: commit enum ADD VALUE before INSERT in migration 037 #386

Merged
forgejo_admin merged 1 commit from 384-fix-migration-037-enum-transaction into main 2026-04-07 19:28:59 +00:00

Summary

Migration 037 fails on fresh deploy because PostgreSQL cannot use newly added enum values within the same transaction that added them. This fix uses the established COMMIT/BEGIN pattern to commit enum additions before referencing them in the INSERT.

Changes

  • alembic/versions/037_add_monthly_category_and_first_payment_email.py — Added import sqlalchemy as sa, replaced bare op.execute for ALTER TYPE with conn.execute(sa.text(...)) wrapped in explicit COMMIT/BEGIN to flush enum values before use.

Test Plan

  • File parses cleanly (ast.parse verified)
  • Pattern matches established migrations 011, 012, 018, 027
  • IF NOT EXISTS makes it idempotent (safe for prod where workaround already applied)
  • Fresh deploy should now run migration 037 without transaction conflict

Review Checklist

  • Code follows established migration patterns (011, 012, 018, 027)
  • File parses without errors
  • Idempotent with IF NOT EXISTS
  • Downgrade function unchanged

None.

## Summary Migration 037 fails on fresh deploy because PostgreSQL cannot use newly added enum values within the same transaction that added them. This fix uses the established COMMIT/BEGIN pattern to commit enum additions before referencing them in the INSERT. ## Changes - `alembic/versions/037_add_monthly_category_and_first_payment_email.py` — Added `import sqlalchemy as sa`, replaced bare `op.execute` for ALTER TYPE with `conn.execute(sa.text(...))` wrapped in explicit COMMIT/BEGIN to flush enum values before use. ## Test Plan - File parses cleanly (`ast.parse` verified) - Pattern matches established migrations 011, 012, 018, 027 - `IF NOT EXISTS` makes it idempotent (safe for prod where workaround already applied) - Fresh deploy should now run migration 037 without transaction conflict ## Review Checklist - [x] Code follows established migration patterns (011, 012, 018, 027) - [x] File parses without errors - [x] Idempotent with `IF NOT EXISTS` - [x] Downgrade function unchanged ## Related Notes None. ## Related - Closes forgejo_admin/basketball-api#384
fix: commit enum ADD VALUE before INSERT in migration 037
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
141c28e602
PostgreSQL requires new enum values to be committed before they can be
referenced in the same transaction. Uses COMMIT/BEGIN pattern consistent
with migrations 011, 012, 018, 027.

Closes forgejo_admin/basketball-api#384

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Author
Owner

QA Review

Scope: 1 file, 7 additions, 3 deletions. Migration 037 fix for enum ADD VALUE + INSERT in same transaction.

Findings:

  • COMMIT/BEGIN pattern is correct. PostgreSQL requires enum ADD VALUE to be committed before the new value can be referenced in DML. This is the only migration in the repo that both adds enum values and uses them in an INSERT, so it is the only one that needs this pattern.
  • sa.text() wrapping is correct for conn.execute() (SQLAlchemy 2.x requires text objects).
  • IF NOT EXISTS ensures idempotency -- safe to run on prod where the workaround was already applied manually.
  • Downgrade function is unchanged and unaffected.
  • import sqlalchemy as sa added correctly.
  • File parses cleanly.

No issues found.

VERDICT: APPROVED

## QA Review **Scope:** 1 file, 7 additions, 3 deletions. Migration 037 fix for enum ADD VALUE + INSERT in same transaction. **Findings:** - COMMIT/BEGIN pattern is correct. PostgreSQL requires enum ADD VALUE to be committed before the new value can be referenced in DML. This is the only migration in the repo that both adds enum values and uses them in an INSERT, so it is the only one that needs this pattern. - `sa.text()` wrapping is correct for `conn.execute()` (SQLAlchemy 2.x requires text objects). - `IF NOT EXISTS` ensures idempotency -- safe to run on prod where the workaround was already applied manually. - Downgrade function is unchanged and unaffected. - `import sqlalchemy as sa` added correctly. - File parses cleanly. **No issues found.** VERDICT: APPROVED
forgejo_admin deleted branch 384-fix-migration-037-enum-transaction 2026-04-07 19:28:59 +00:00
Sign in to join this conversation.
No description provided.