From f8fecf79ac4723359a496e05b88e03a878607988 Mon Sep 17 00:00:00 2001 From: jlightner Date: Sat, 4 Apr 2026 07:19:01 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20use=20pure=20SQL=20for=20migrations=2001?= =?UTF-8?q?9/020=20=E2=80=94=20bypasses=20all=20SQLAlchemy=20enum=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../versions/019_add_highlight_candidates.py | 36 +++++++++---------- .../020_add_chapter_status_and_sort_order.py | 12 +++---- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/alembic/versions/019_add_highlight_candidates.py b/alembic/versions/019_add_highlight_candidates.py index f22e999..7bfa9b9 100644 --- a/alembic/versions/019_add_highlight_candidates.py +++ b/alembic/versions/019_add_highlight_candidates.py @@ -16,26 +16,24 @@ depends_on = None def upgrade() -> None: - # Create enum via raw SQL + # Pure SQL to avoid all SQLAlchemy enum creation hooks op.execute("CREATE TYPE highlight_status AS ENUM ('candidate', 'approved', 'rejected')") - - op.create_table( - "highlight_candidates", - sa.Column("id", UUID(as_uuid=True), primary_key=True, server_default=sa.text("gen_random_uuid()")), - sa.Column("key_moment_id", UUID(as_uuid=True), sa.ForeignKey("key_moments.id", ondelete="CASCADE"), nullable=False, unique=True), - sa.Column("source_video_id", UUID(as_uuid=True), sa.ForeignKey("source_videos.id", ondelete="CASCADE"), nullable=False), - 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", sa.String(20), 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), - ) - # Change column type to use the enum - op.execute("ALTER TABLE highlight_candidates ALTER COLUMN status TYPE highlight_status USING status::highlight_status") - op.create_index("ix_highlight_candidates_source_video_id", "highlight_candidates", ["source_video_id"]) - op.create_index("ix_highlight_candidates_score_desc", "highlight_candidates", [sa.text("score DESC")]) - op.create_index("ix_highlight_candidates_status", "highlight_candidates", ["status"]) + op.execute(""" + CREATE TABLE highlight_candidates ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + key_moment_id UUID NOT NULL UNIQUE REFERENCES key_moments(id) ON DELETE CASCADE, + source_video_id UUID NOT NULL REFERENCES source_videos(id) ON DELETE CASCADE, + score FLOAT NOT NULL, + score_breakdown JSONB, + duration_secs FLOAT NOT NULL, + status highlight_status NOT NULL DEFAULT 'candidate', + created_at TIMESTAMP NOT NULL DEFAULT now(), + updated_at TIMESTAMP NOT NULL DEFAULT now() + ) + """) + op.execute("CREATE INDEX ix_highlight_candidates_source_video_id ON highlight_candidates (source_video_id)") + op.execute("CREATE INDEX ix_highlight_candidates_score_desc ON highlight_candidates (score DESC)") + op.execute("CREATE INDEX ix_highlight_candidates_status ON highlight_candidates (status)") def downgrade() -> None: 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 c981bfd..ba67a09 100644 --- a/alembic/versions/020_add_chapter_status_and_sort_order.py +++ b/alembic/versions/020_add_chapter_status_and_sort_order.py @@ -15,15 +15,11 @@ depends_on = None def upgrade() -> None: - # Create enum via raw SQL + # Pure SQL to avoid SQLAlchemy enum creation hooks op.execute("CREATE TYPE chapter_status AS ENUM ('draft', 'approved', 'hidden')") - - op.add_column( - "key_moments", - sa.Column("chapter_status", sa.String(20), nullable=False, server_default="draft"), - ) - # Change column type to use the enum - op.execute("ALTER TABLE key_moments ALTER COLUMN chapter_status TYPE chapter_status USING chapter_status::chapter_status") + op.execute("ALTER TABLE key_moments ADD COLUMN chapter_status chapter_status NOT NULL DEFAULT 'draft'") + op.execute("ALTER TABLE key_moments ADD COLUMN sort_order INTEGER NOT NULL DEFAULT 0") + op.execute("CREATE INDEX ix_key_moments_chapter_status ON key_moments (chapter_status)") op.add_column( "key_moments", sa.Column("sort_order", sa.Integer, nullable=False, server_default="0"),