|
| 1 | +"""Add mosaic session membership table and related columns. |
| 2 | +
|
| 3 | +Revision ID: 0013 |
| 4 | +Revises: 0012 |
| 5 | +""" |
| 6 | + |
| 7 | +from alembic import op |
| 8 | +import sqlalchemy as sa |
| 9 | +from sqlalchemy.dialects.postgresql import UUID, JSONB |
| 10 | + |
| 11 | + |
| 12 | +revision = "0013" |
| 13 | +down_revision = "0012" |
| 14 | +branch_labels = None |
| 15 | +depends_on = None |
| 16 | + |
| 17 | + |
| 18 | +def _column_exists(table_name: str, column_name: str) -> bool: |
| 19 | + bind = op.get_bind() |
| 20 | + insp = sa.inspect(bind) |
| 21 | + columns = [c["name"] for c in insp.get_columns(table_name)] |
| 22 | + return column_name in columns |
| 23 | + |
| 24 | + |
| 25 | +def _table_exists(table_name: str) -> bool: |
| 26 | + bind = op.get_bind() |
| 27 | + insp = sa.inspect(bind) |
| 28 | + return table_name in insp.get_table_names() |
| 29 | + |
| 30 | + |
| 31 | +def upgrade() -> None: |
| 32 | + # 1. Create mosaic_panel_sessions table |
| 33 | + if not _table_exists("mosaic_panel_sessions"): |
| 34 | + op.create_table( |
| 35 | + "mosaic_panel_sessions", |
| 36 | + sa.Column("id", UUID(as_uuid=True), primary_key=True, server_default=sa.text("gen_random_uuid()")), |
| 37 | + sa.Column("panel_id", UUID(as_uuid=True), sa.ForeignKey("mosaic_panels.id", ondelete="CASCADE"), nullable=False), |
| 38 | + sa.Column("session_date", sa.Date, nullable=False), |
| 39 | + sa.Column("status", sa.String(20), nullable=False, server_default="available"), |
| 40 | + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()), |
| 41 | + sa.UniqueConstraint("panel_id", "session_date", name="uq_mosaic_panel_sessions_panel_date"), |
| 42 | + ) |
| 43 | + op.create_index("ix_mosaic_panel_sessions_panel_id", "mosaic_panel_sessions", ["panel_id"]) |
| 44 | + op.create_index("ix_mosaic_panel_sessions_status", "mosaic_panel_sessions", ["status"]) |
| 45 | + |
| 46 | + # 2. Add needs_review to mosaics |
| 47 | + if not _column_exists("mosaics", "needs_review"): |
| 48 | + op.add_column("mosaics", sa.Column("needs_review", sa.Boolean, nullable=False, server_default="false")) |
| 49 | + |
| 50 | + # 3. Add session_dates to mosaic_suggestions |
| 51 | + if not _column_exists("mosaic_suggestions", "session_dates"): |
| 52 | + op.add_column("mosaic_suggestions", sa.Column("session_dates", JSONB, nullable=True)) |
| 53 | + |
| 54 | + # 4. Data migration: seed session records for existing mosaic panels |
| 55 | + conn = op.get_bind() |
| 56 | + panels = conn.execute(sa.text( |
| 57 | + "SELECT mp.id AS panel_id, mp.target_id, mp.object_pattern, m.id AS mosaic_id " |
| 58 | + "FROM mosaic_panels mp JOIN mosaics m ON mp.mosaic_id = m.id" |
| 59 | + )).fetchall() |
| 60 | + |
| 61 | + for panel in panels: |
| 62 | + panel_id = panel.panel_id |
| 63 | + target_id = panel.target_id |
| 64 | + pattern = panel.object_pattern |
| 65 | + |
| 66 | + if pattern: |
| 67 | + dates = conn.execute(sa.text( |
| 68 | + "SELECT DISTINCT session_date FROM images " |
| 69 | + "WHERE resolved_target_id = :tid AND image_type = 'LIGHT' " |
| 70 | + "AND raw_headers->>'OBJECT' ILIKE :pat AND session_date IS NOT NULL" |
| 71 | + ), {"tid": target_id, "pat": pattern}).fetchall() |
| 72 | + else: |
| 73 | + dates = conn.execute(sa.text( |
| 74 | + "SELECT DISTINCT session_date FROM images " |
| 75 | + "WHERE resolved_target_id = :tid AND image_type = 'LIGHT' " |
| 76 | + "AND session_date IS NOT NULL" |
| 77 | + ), {"tid": target_id}).fetchall() |
| 78 | + |
| 79 | + for row in dates: |
| 80 | + conn.execute(sa.text( |
| 81 | + "INSERT INTO mosaic_panel_sessions (id, panel_id, session_date, status) " |
| 82 | + "VALUES (gen_random_uuid(), :panel_id, :session_date, 'available') " |
| 83 | + "ON CONFLICT (panel_id, session_date) DO NOTHING" |
| 84 | + ), {"panel_id": panel_id, "session_date": row.session_date}) |
| 85 | + |
| 86 | + # Mark all existing mosaics as needs_review |
| 87 | + conn.execute(sa.text("UPDATE mosaics SET needs_review = true")) |
| 88 | + |
| 89 | + |
| 90 | +def downgrade() -> None: |
| 91 | + op.drop_table("mosaic_panel_sessions") |
| 92 | + if _column_exists("mosaics", "needs_review"): |
| 93 | + op.drop_column("mosaics", "needs_review") |
| 94 | + if _column_exists("mosaic_suggestions", "session_dates"): |
| 95 | + op.drop_column("mosaic_suggestions", "session_dates") |
0 commit comments