fix: make migrations 019/020 fully idempotent with IF NOT EXISTS guards

This commit is contained in:
jlightner 2026-04-04 07:20:48 +00:00
parent f8fecf79ac
commit ed8bdedbc9
2 changed files with 10 additions and 10 deletions

View file

@ -16,10 +16,10 @@ depends_on = None
def upgrade() -> None: def upgrade() -> None:
# Pure SQL to avoid all SQLAlchemy enum creation hooks # Pure SQL — idempotent with IF NOT EXISTS / exception guards
op.execute("CREATE TYPE highlight_status AS ENUM ('candidate', 'approved', 'rejected')") op.execute("DO $$ BEGIN CREATE TYPE highlight_status AS ENUM ('candidate', 'approved', 'rejected'); EXCEPTION WHEN duplicate_object THEN NULL; END $$")
op.execute(""" op.execute("""
CREATE TABLE highlight_candidates ( CREATE TABLE IF NOT EXISTS highlight_candidates (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
key_moment_id UUID NOT NULL UNIQUE REFERENCES key_moments(id) ON DELETE CASCADE, 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, source_video_id UUID NOT NULL REFERENCES source_videos(id) ON DELETE CASCADE,
@ -31,9 +31,9 @@ def upgrade() -> None:
updated_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 IF NOT EXISTS 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 IF NOT EXISTS ix_highlight_candidates_score_desc ON highlight_candidates (score DESC)")
op.execute("CREATE INDEX ix_highlight_candidates_status ON highlight_candidates (status)") op.execute("CREATE INDEX IF NOT EXISTS ix_highlight_candidates_status ON highlight_candidates (status)")
def downgrade() -> None: def downgrade() -> None:

View file

@ -16,10 +16,10 @@ depends_on = None
def upgrade() -> None: def upgrade() -> None:
# Pure SQL to avoid SQLAlchemy enum creation hooks # Pure SQL to avoid SQLAlchemy enum creation hooks
op.execute("CREATE TYPE chapter_status AS ENUM ('draft', 'approved', 'hidden')") op.execute("DO $$ BEGIN CREATE TYPE chapter_status AS ENUM ('draft', 'approved', 'hidden'); EXCEPTION WHEN duplicate_object THEN NULL; END $$")
op.execute("ALTER TABLE key_moments ADD COLUMN chapter_status chapter_status NOT NULL DEFAULT 'draft'") op.execute("ALTER TABLE key_moments ADD COLUMN IF NOT EXISTS 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("ALTER TABLE key_moments ADD COLUMN IF NOT EXISTS sort_order INTEGER NOT NULL DEFAULT 0")
op.execute("CREATE INDEX ix_key_moments_chapter_status ON key_moments (chapter_status)") op.execute("CREATE INDEX IF NOT EXISTS ix_key_moments_chapter_status ON key_moments (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"),