chrysopedia/alembic/versions/019_add_highlight_candidates.py

49 lines
2.4 KiB
Python

"""Add highlight_candidates table for highlight detection scoring.
Revision ID: 019_add_highlight_candidates
Revises: 018_add_impersonation_log
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID
revision = "019_add_highlight_candidates"
down_revision = "018_add_impersonation_log"
branch_labels = None
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)
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", hl_col_type, 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),
)
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"])
def downgrade() -> None:
op.drop_index("ix_highlight_candidates_status")
op.drop_index("ix_highlight_candidates_score_desc")
op.drop_index("ix_highlight_candidates_source_video_id")
op.drop_table("highlight_candidates")
sa.Enum(name="highlight_status").drop(op.get_bind(), checkfirst=True)