"""Widen varchar(36) ID columns to varchar(128) — idempotent repair. Migration 0030 was stamped before it ran on staging (same stamp-and-retry escape hatch issue as 0035/0036). This migration re-applies those ALTERs using DO $$ conditional blocks that are no-ops when the column is already the right size. Also widens musehub_background_jobs.repo_id which 0030 missed. Revision ID: 0037 Revises: 0036 """ from __future__ import annotations from alembic import op from sqlalchemy import text revision = "0037" down_revision = "0036" branch_labels = None depends_on = None _WIDEN = [ ("musehub_background_jobs", "job_id"), ("musehub_background_jobs", "repo_id"), ("musehub_domain_installs", "install_id"), ("musehub_issue_events", "event_id"), ("musehub_webhook_deliveries", "delivery_id"), ] def upgrade() -> None: for table, col in _WIDEN: op.execute(text(f""" DO $$ BEGIN IF EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_name = '{table}' AND column_name = '{col}' AND character_maximum_length < 128 ) THEN ALTER TABLE {table} ALTER COLUMN {col} TYPE VARCHAR(128); END IF; END $$ """)) def downgrade() -> None: # Narrow columns back to VARCHAR(36) — the original width before the repair. for table, col in _WIDEN: op.execute(text(f""" DO $$ BEGIN IF EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_name = '{table}' AND column_name = '{col}' AND character_maximum_length >= 128 ) THEN ALTER TABLE {table} ALTER COLUMN {col} TYPE VARCHAR(36); END IF; END $$ """))