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:
# 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:

View file

@ -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"),