From a9b7b7ead594b183cd97c087756f032df7b09324 Mon Sep 17 00:00:00 2001 From: Renn F Date: Thu, 18 Jun 2026 07:30:09 +0200 Subject: [PATCH] fix(migration): commit the grok enum value before seeding (autocommit_block) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CI's "Apply database migrations" failed with asyncpg UnsafeNewEnumValueUsageError: alembic runs the whole upgrade in a single transaction, so migration 039's INSERT used 'grok' in the same transaction that 038 added it — which Postgres forbids. Splitting into two migration files did not help (one transaction spans both). Wrap the ALTER TYPE ADD VALUE in op.get_context().autocommit_block() so the value commits before 039 (and any later migration) uses it. Still renders in offline --sql, so the enum-migration-parity test is unaffected. --- alembic/versions/038_modelprovider_grok.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/alembic/versions/038_modelprovider_grok.py b/alembic/versions/038_modelprovider_grok.py index 6f72d0c2..d80dbe5b 100644 --- a/alembic/versions/038_modelprovider_grok.py +++ b/alembic/versions/038_modelprovider_grok.py @@ -22,9 +22,15 @@ depends_on = None def upgrade() -> None: - # Unguarded (renders in offline --sql so the enum-migration-parity test - # sees it) and idempotent. PG 16 permits ADD VALUE inside a transaction. - op.execute("ALTER TYPE modelprovider ADD VALUE IF NOT EXISTS 'grok'") + # The new value must be COMMITTED before migration 039 inserts a row using + # it: alembic runs the whole upgrade in a single transaction, and Postgres + # forbids using a freshly added enum value in the same transaction that + # added it (UnsafeNewEnumValueUsageError). autocommit_block commits the + # ALTER on its own so 'grok' is usable downstream. Still renders the ALTER + # TYPE in offline --sql, so the enum-migration-parity test sees it. + # Idempotent via IF NOT EXISTS. + with op.get_context().autocommit_block(): + op.execute("ALTER TYPE modelprovider ADD VALUE IF NOT EXISTS 'grok'") def downgrade() -> None: