From b9f042f400c4052a3dbf2ce4016f7f7c80101fad Mon Sep 17 00:00:00 2001 From: jlightner Date: Sat, 4 Apr 2026 07:16:22 +0000 Subject: [PATCH] fix: use raw SQL for enum creation in migrations 019/020 to avoid SQLAlchemy double-create --- alembic/versions/019_add_highlight_candidates.py | 11 +++-------- .../versions/020_add_chapter_status_and_sort_order.py | 10 +++------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/alembic/versions/019_add_highlight_candidates.py b/alembic/versions/019_add_highlight_candidates.py index f2b32ed..277b030 100644 --- a/alembic/versions/019_add_highlight_candidates.py +++ b/alembic/versions/019_add_highlight_candidates.py @@ -16,13 +16,8 @@ depends_on = None def upgrade() -> None: - # Create the highlight_status enum type (create_type=False on the column - # prevents SQLAlchemy's before_create event from double-creating it) - highlight_status = sa.Enum("candidate", "approved", "rejected", name="highlight_status", create_constraint=True) - highlight_status.create(op.get_bind(), checkfirst=True) - - # Use create_type=False so create_table doesn't try to recreate the enum - hl_col_type = sa.Enum("candidate", "approved", "rejected", name="highlight_status", create_type=False) + # Create enum via raw SQL to avoid SQLAlchemy's double-creation issue + op.execute("CREATE TYPE highlight_status AS ENUM ('candidate', 'approved', 'rejected')") op.create_table( "highlight_candidates", @@ -32,7 +27,7 @@ def upgrade() -> None: sa.Column("score", sa.Float, nullable=False), sa.Column("score_breakdown", sa.dialects.postgresql.JSONB, nullable=True), sa.Column("duration_secs", sa.Float, nullable=False), - sa.Column("status", hl_col_type, nullable=False, server_default="candidate"), + sa.Column("status", sa.Enum("candidate", "approved", "rejected", name="highlight_status", create_type=False), nullable=False, server_default="candidate"), sa.Column("created_at", sa.DateTime, server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.DateTime, server_default=sa.func.now(), nullable=False), ) diff --git a/alembic/versions/020_add_chapter_status_and_sort_order.py b/alembic/versions/020_add_chapter_status_and_sort_order.py index 9a648ca..2bccd3a 100644 --- a/alembic/versions/020_add_chapter_status_and_sort_order.py +++ b/alembic/versions/020_add_chapter_status_and_sort_order.py @@ -15,16 +15,12 @@ depends_on = None def upgrade() -> None: - # Create the chapter_status enum type - chapter_status = sa.Enum("draft", "approved", "hidden", name="chapter_status", create_constraint=True) - chapter_status.create(op.get_bind(), checkfirst=True) - - # Use create_type=False to prevent double-creation during add_column - ch_col_type = sa.Enum("draft", "approved", "hidden", name="chapter_status", create_type=False) + # Create enum via raw SQL to avoid SQLAlchemy's double-creation issue + op.execute("CREATE TYPE chapter_status AS ENUM ('draft', 'approved', 'hidden')") op.add_column( "key_moments", - sa.Column("chapter_status", ch_col_type, nullable=False, server_default="draft"), + sa.Column("chapter_status", sa.Enum("draft", "approved", "hidden", name="chapter_status", create_type=False), nullable=False, server_default="draft"), ) op.add_column( "key_moments",