fix: use pure SQL for migrations 019/020 — bypasses all SQLAlchemy enum hooks

This commit is contained in:
jlightner 2026-04-04 07:19:01 +00:00
parent 9c307c663d
commit f8fecf79ac
2 changed files with 21 additions and 27 deletions

View file

@ -16,26 +16,24 @@ depends_on = None
def upgrade() -> 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.execute("CREATE TYPE highlight_status AS ENUM ('candidate', 'approved', 'rejected')")
op.execute("""
op.create_table( CREATE TABLE highlight_candidates (
"highlight_candidates", id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
sa.Column("id", UUID(as_uuid=True), primary_key=True, server_default=sa.text("gen_random_uuid()")), key_moment_id UUID NOT NULL UNIQUE REFERENCES key_moments(id) ON DELETE CASCADE,
sa.Column("key_moment_id", UUID(as_uuid=True), sa.ForeignKey("key_moments.id", ondelete="CASCADE"), nullable=False, unique=True), source_video_id UUID NOT NULL REFERENCES source_videos(id) ON DELETE CASCADE,
sa.Column("source_video_id", UUID(as_uuid=True), sa.ForeignKey("source_videos.id", ondelete="CASCADE"), nullable=False), score FLOAT NOT NULL,
sa.Column("score", sa.Float, nullable=False), score_breakdown JSONB,
sa.Column("score_breakdown", sa.dialects.postgresql.JSONB, nullable=True), duration_secs FLOAT NOT NULL,
sa.Column("duration_secs", sa.Float, nullable=False), status highlight_status NOT NULL DEFAULT 'candidate',
sa.Column("status", sa.String(20), nullable=False, server_default="candidate"), created_at TIMESTAMP NOT NULL DEFAULT now(),
sa.Column("created_at", sa.DateTime, server_default=sa.func.now(), nullable=False), updated_at TIMESTAMP NOT NULL DEFAULT now()
sa.Column("updated_at", sa.DateTime, server_default=sa.func.now(), nullable=False), )
) """)
# Change column type to use the enum op.execute("CREATE INDEX ix_highlight_candidates_source_video_id ON highlight_candidates (source_video_id)")
op.execute("ALTER TABLE highlight_candidates ALTER COLUMN status TYPE highlight_status USING status::highlight_status") op.execute("CREATE INDEX ix_highlight_candidates_score_desc ON highlight_candidates (score DESC)")
op.create_index("ix_highlight_candidates_source_video_id", "highlight_candidates", ["source_video_id"]) op.execute("CREATE INDEX ix_highlight_candidates_status ON highlight_candidates (status)")
op.create_index("ix_highlight_candidates_score_desc", "highlight_candidates", [sa.text("score DESC")])
op.create_index("ix_highlight_candidates_status", "highlight_candidates", ["status"])
def downgrade() -> None: def downgrade() -> None:

View file

@ -15,15 +15,11 @@ depends_on = None
def upgrade() -> 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.execute("CREATE TYPE chapter_status AS ENUM ('draft', 'approved', 'hidden')")
op.execute("ALTER TABLE key_moments ADD COLUMN chapter_status chapter_status NOT NULL DEFAULT 'draft'")
op.add_column( op.execute("ALTER TABLE key_moments ADD COLUMN sort_order INTEGER NOT NULL DEFAULT 0")
"key_moments", op.execute("CREATE INDEX ix_key_moments_chapter_status ON key_moments (chapter_status)")
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.add_column( op.add_column(
"key_moments", "key_moments",
sa.Column("sort_order", sa.Integer, nullable=False, server_default="0"), sa.Column("sort_order", sa.Integer, nullable=False, server_default="0"),