fix: use String column + ALTER TYPE cast for enum migrations (avoids SQLAlchemy before_create hook)

This commit is contained in:
jlightner 2026-04-04 07:17:34 +00:00
parent b9f042f400
commit 9c307c663d
2 changed files with 8 additions and 4 deletions

View file

@ -16,7 +16,7 @@ depends_on = None
def upgrade() -> None:
# Create enum via raw SQL to avoid SQLAlchemy's double-creation issue
# Create enum via raw SQL
op.execute("CREATE TYPE highlight_status AS ENUM ('candidate', 'approved', 'rejected')")
op.create_table(
@ -27,10 +27,12 @@ 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", sa.Enum("candidate", "approved", "rejected", name="highlight_status", create_type=False), nullable=False, server_default="candidate"),
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"])

View file

@ -15,13 +15,15 @@ depends_on = None
def upgrade() -> None:
# Create enum via raw SQL to avoid SQLAlchemy's double-creation issue
# Create enum via raw SQL
op.execute("CREATE TYPE chapter_status AS ENUM ('draft', 'approved', 'hidden')")
op.add_column(
"key_moments",
sa.Column("chapter_status", sa.Enum("draft", "approved", "hidden", name="chapter_status", create_type=False), nullable=False, server_default="draft"),
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(
"key_moments",
sa.Column("sort_order", sa.Integer, nullable=False, server_default="0"),