|
| 1 | +"""add model_selection_candidate and async progress columns |
| 2 | +
|
| 3 | +Revision ID: d3e4f5a6b7c8 |
| 4 | +Revises: b667d321603c |
| 5 | +Create Date: 2026-06-01 09:30:00.000000 |
| 6 | +
|
| 7 | +Slice B of the Forecast Champion Selector (issue #360). Converts the selection |
| 8 | +run into a DB-backed async LRO: |
| 9 | +
|
| 10 | +- creates ``model_selection_candidate`` (one row per candidate, FK CASCADE to |
| 11 | + ``model_selection_run.selection_id``) carrying per-candidate status, result |
| 12 | + JSONB, error, and timing — the live-progress + audit surface; |
| 13 | +- adds ``started_at`` + the four final count columns to ``model_selection_run``; |
| 14 | +- widens the run status CheckConstraint to include ``'cancelled'`` (forward-only |
| 15 | + drop + recreate of the named constraint). |
| 16 | +
|
| 17 | +Mirrors ``c1d2e3f40512_create_batch_tables`` for JSONB / index / FK style. |
| 18 | +""" |
| 19 | + |
| 20 | +from collections.abc import Sequence |
| 21 | + |
| 22 | +import sqlalchemy as sa |
| 23 | +from sqlalchemy.dialects import postgresql |
| 24 | + |
| 25 | +from alembic import op |
| 26 | + |
| 27 | +# revision identifiers, used by Alembic. |
| 28 | +revision: str = "d3e4f5a6b7c8" |
| 29 | +down_revision: str | None = "b667d321603c" |
| 30 | +branch_labels: str | Sequence[str] | None = None |
| 31 | +depends_on: str | Sequence[str] | None = None |
| 32 | + |
| 33 | +_OLD_RUN_STATUS = "status IN ('pending', 'running', 'completed', 'partial', 'failed')" |
| 34 | +_NEW_RUN_STATUS = ( |
| 35 | + "status IN ('pending', 'running', 'completed', 'partial', 'failed', 'cancelled')" |
| 36 | +) |
| 37 | + |
| 38 | + |
| 39 | +def upgrade() -> None: |
| 40 | + """Apply migration.""" |
| 41 | + # ------------------------------------------------------------------ |
| 42 | + # 1. Widen the run status CheckConstraint to include 'cancelled'. |
| 43 | + # ------------------------------------------------------------------ |
| 44 | + op.drop_constraint( |
| 45 | + "ck_model_selection_run_valid_status", |
| 46 | + "model_selection_run", |
| 47 | + type_="check", |
| 48 | + ) |
| 49 | + op.create_check_constraint( |
| 50 | + "ck_model_selection_run_valid_status", |
| 51 | + "model_selection_run", |
| 52 | + _NEW_RUN_STATUS, |
| 53 | + ) |
| 54 | + |
| 55 | + # ------------------------------------------------------------------ |
| 56 | + # 2. Additive progress columns on the parent run. |
| 57 | + # ------------------------------------------------------------------ |
| 58 | + op.add_column( |
| 59 | + "model_selection_run", |
| 60 | + sa.Column("started_at", sa.DateTime(timezone=True), nullable=True), |
| 61 | + ) |
| 62 | + op.add_column( |
| 63 | + "model_selection_run", |
| 64 | + sa.Column("total_candidates", sa.Integer(), nullable=False, server_default="0"), |
| 65 | + ) |
| 66 | + op.add_column( |
| 67 | + "model_selection_run", |
| 68 | + sa.Column( |
| 69 | + "completed_candidates", sa.Integer(), nullable=False, server_default="0" |
| 70 | + ), |
| 71 | + ) |
| 72 | + op.add_column( |
| 73 | + "model_selection_run", |
| 74 | + sa.Column("failed_candidates", sa.Integer(), nullable=False, server_default="0"), |
| 75 | + ) |
| 76 | + op.add_column( |
| 77 | + "model_selection_run", |
| 78 | + sa.Column( |
| 79 | + "cancelled_candidates", sa.Integer(), nullable=False, server_default="0" |
| 80 | + ), |
| 81 | + ) |
| 82 | + |
| 83 | + # ------------------------------------------------------------------ |
| 84 | + # 3. Per-candidate execution child table (FK CASCADE on selection_id). |
| 85 | + # ------------------------------------------------------------------ |
| 86 | + op.create_table( |
| 87 | + "model_selection_candidate", |
| 88 | + sa.Column("id", sa.Integer(), nullable=False), |
| 89 | + sa.Column("candidate_id", sa.String(length=32), nullable=False), |
| 90 | + sa.Column("selection_id", sa.String(length=32), nullable=False), |
| 91 | + sa.Column("ordinal", sa.Integer(), nullable=False), |
| 92 | + sa.Column("model_type", sa.String(length=40), nullable=False), |
| 93 | + sa.Column("params", postgresql.JSONB(astext_type=sa.Text()), nullable=False), |
| 94 | + sa.Column("status", sa.String(length=20), nullable=False), |
| 95 | + sa.Column("result", postgresql.JSONB(astext_type=sa.Text()), nullable=True), |
| 96 | + sa.Column("error_message", sa.String(length=2000), nullable=True), |
| 97 | + sa.Column("error_type", sa.String(length=100), nullable=True), |
| 98 | + sa.Column("started_at", sa.DateTime(timezone=True), nullable=True), |
| 99 | + sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True), |
| 100 | + sa.Column("duration_ms", sa.Integer(), nullable=True), |
| 101 | + sa.Column( |
| 102 | + "created_at", |
| 103 | + sa.DateTime(timezone=True), |
| 104 | + server_default=sa.text("now()"), |
| 105 | + nullable=False, |
| 106 | + ), |
| 107 | + sa.Column( |
| 108 | + "updated_at", |
| 109 | + sa.DateTime(timezone=True), |
| 110 | + server_default=sa.text("now()"), |
| 111 | + nullable=False, |
| 112 | + ), |
| 113 | + sa.CheckConstraint( |
| 114 | + "status IN ('pending', 'running', 'completed', 'failed', 'cancelled')", |
| 115 | + name="ck_model_selection_candidate_valid_status", |
| 116 | + ), |
| 117 | + sa.ForeignKeyConstraint( |
| 118 | + ["selection_id"], |
| 119 | + ["model_selection_run.selection_id"], |
| 120 | + ondelete="CASCADE", |
| 121 | + ), |
| 122 | + sa.PrimaryKeyConstraint("id"), |
| 123 | + ) |
| 124 | + op.create_index( |
| 125 | + op.f("ix_model_selection_candidate_candidate_id"), |
| 126 | + "model_selection_candidate", |
| 127 | + ["candidate_id"], |
| 128 | + unique=True, |
| 129 | + ) |
| 130 | + op.create_index( |
| 131 | + op.f("ix_model_selection_candidate_selection_id"), |
| 132 | + "model_selection_candidate", |
| 133 | + ["selection_id"], |
| 134 | + unique=False, |
| 135 | + ) |
| 136 | + op.create_index( |
| 137 | + op.f("ix_model_selection_candidate_status"), |
| 138 | + "model_selection_candidate", |
| 139 | + ["status"], |
| 140 | + unique=False, |
| 141 | + ) |
| 142 | + op.create_index( |
| 143 | + "ix_model_selection_candidate_selection_status", |
| 144 | + "model_selection_candidate", |
| 145 | + ["selection_id", "status"], |
| 146 | + unique=False, |
| 147 | + ) |
| 148 | + |
| 149 | + |
| 150 | +def downgrade() -> None: |
| 151 | + """Revert migration.""" |
| 152 | + op.drop_index( |
| 153 | + "ix_model_selection_candidate_selection_status", |
| 154 | + table_name="model_selection_candidate", |
| 155 | + ) |
| 156 | + op.drop_index( |
| 157 | + op.f("ix_model_selection_candidate_status"), |
| 158 | + table_name="model_selection_candidate", |
| 159 | + ) |
| 160 | + op.drop_index( |
| 161 | + op.f("ix_model_selection_candidate_selection_id"), |
| 162 | + table_name="model_selection_candidate", |
| 163 | + ) |
| 164 | + op.drop_index( |
| 165 | + op.f("ix_model_selection_candidate_candidate_id"), |
| 166 | + table_name="model_selection_candidate", |
| 167 | + ) |
| 168 | + op.drop_table("model_selection_candidate") |
| 169 | + |
| 170 | + op.drop_column("model_selection_run", "cancelled_candidates") |
| 171 | + op.drop_column("model_selection_run", "failed_candidates") |
| 172 | + op.drop_column("model_selection_run", "completed_candidates") |
| 173 | + op.drop_column("model_selection_run", "total_candidates") |
| 174 | + op.drop_column("model_selection_run", "started_at") |
| 175 | + |
| 176 | + op.drop_constraint( |
| 177 | + "ck_model_selection_run_valid_status", |
| 178 | + "model_selection_run", |
| 179 | + type_="check", |
| 180 | + ) |
| 181 | + op.create_check_constraint( |
| 182 | + "ck_model_selection_run_valid_status", |
| 183 | + "model_selection_run", |
| 184 | + _OLD_RUN_STATUS, |
| 185 | + ) |
0 commit comments