|
| 1 | +"""unify_activity_status |
| 2 | +
|
| 3 | +Revision ID: 447c8883c88f |
| 4 | +Revises: 07064e01c345 |
| 5 | +Create Date: 2026-01-20 17:59:31.059678 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Sequence, Union |
| 10 | + |
| 11 | +from alembic import op |
| 12 | +import sqlalchemy as sa |
| 13 | +from sqlalchemy.dialects import postgresql |
| 14 | + |
| 15 | +from sqlalchemy import Text, text, inspect |
| 16 | +import app.db.types |
| 17 | +from app.db.model import Activity |
| 18 | + |
| 19 | +# revision identifiers, used by Alembic. |
| 20 | +revision: str = "447c8883c88f" |
| 21 | +down_revision: Union[str, None] = "07064e01c345" |
| 22 | +branch_labels: Union[str, Sequence[str], None] = None |
| 23 | +depends_on: Union[str, Sequence[str], None] = None |
| 24 | + |
| 25 | + |
| 26 | +TABLES_WITHOUT_STATUS = [ |
| 27 | + "analysis_notebook_execution", |
| 28 | + "calibration", |
| 29 | + "circuit_extraction_config_generation", |
| 30 | + "ion_channel_modeling_config_generation", |
| 31 | + "simulation_generation", |
| 32 | + "skeletonization_config_generation", |
| 33 | + "validation", |
| 34 | +] |
| 35 | + |
| 36 | +# move status from these tables to the activity parent table |
| 37 | +TABLES_TO_MOVE_STATUS = [ |
| 38 | + { |
| 39 | + "table": "circuit_extraction_execution", |
| 40 | + "column": "status", |
| 41 | + "enum": { |
| 42 | + "name": "circuit_extraction_execution_status", |
| 43 | + "values": ["created", "pending", "running", "done", "error"], |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + "table": "skeletonization_execution", |
| 48 | + "column": "status", |
| 49 | + "enum": { |
| 50 | + "name": "skeletonizationexecutionstatus", |
| 51 | + "values": ["created", "pending", "running", "done", "error"], |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + "table": "ion_channel_modeling_execution", |
| 56 | + "column": "status", |
| 57 | + "enum": { |
| 58 | + "name": "ion_channel_modeling_execution_status", |
| 59 | + "values": ["created", "pending", "running", "done", "error"], |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + "table": "simulation_execution", |
| 64 | + "column": "status", |
| 65 | + "enum": { |
| 66 | + "name": "simulation_execution_status", |
| 67 | + "values": ["created", "pending", "running", "done", "error", "cancelled"], |
| 68 | + }, |
| 69 | + }, |
| 70 | +] |
| 71 | + |
| 72 | + |
| 73 | +# remap table status to activity status without moving it |
| 74 | +TABLES_TO_DROP_STATUS = [ |
| 75 | + { |
| 76 | + "table": "single_neuron_simulation", |
| 77 | + "column": "status", |
| 78 | + "enum": { |
| 79 | + "name": "singleneuronsimulationstatus", |
| 80 | + "values": ["started", "failure", "success"], |
| 81 | + "default": "success", |
| 82 | + }, |
| 83 | + }, |
| 84 | + { |
| 85 | + "table": "single_neuron_synaptome_simulation", |
| 86 | + "column": "status", |
| 87 | + "enum": { |
| 88 | + "name": "singleneuronsimulationstatus", |
| 89 | + "values": ["started", "failure", "success"], |
| 90 | + "default": "success", |
| 91 | + }, |
| 92 | + }, |
| 93 | +] |
| 94 | + |
| 95 | + |
| 96 | +def _activity_enum(): |
| 97 | + return sa.Enum( |
| 98 | + "created", |
| 99 | + "pending", |
| 100 | + "running", |
| 101 | + "done", |
| 102 | + "error", |
| 103 | + "cancelled", |
| 104 | + name="activitystatus", |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +def _create_activity_status_column(op): |
| 109 | + """Create activity status collumn and fill it with done.""" |
| 110 | + activity_enum = _activity_enum() |
| 111 | + activity_enum.create(op.get_bind()) |
| 112 | + op.add_column( |
| 113 | + "activity", |
| 114 | + sa.Column("status", activity_enum, server_default=sa.text("'done'"), nullable=False), |
| 115 | + ) |
| 116 | + return activity_enum |
| 117 | + |
| 118 | + |
| 119 | +def _using_expr(col: str, mapping: dict[str, str]) -> str: |
| 120 | + if not mapping: |
| 121 | + return f"{col}::text::activitystatus" |
| 122 | + |
| 123 | + cases = "\n".join(f"WHEN '{old}' THEN '{new}'" for old, new in mapping.items()) |
| 124 | + |
| 125 | + return f""" |
| 126 | + ( |
| 127 | + CASE {col}::text |
| 128 | + {cases} |
| 129 | + ELSE {col}::text |
| 130 | + END |
| 131 | + )::activitystatus |
| 132 | + """ |
| 133 | + |
| 134 | + |
| 135 | +def _move_table_statuses(op): |
| 136 | + def _move_status(op, from_table: str, from_column: str, to_table: str, mapping): |
| 137 | + cases = _using_expr(f"{from_table}.{from_column}", mapping) |
| 138 | + op.execute(f""" |
| 139 | + UPDATE {to_table} |
| 140 | + SET status = {cases} |
| 141 | + FROM {from_table} |
| 142 | + WHERE {to_table}.id = {from_table}.id |
| 143 | + """) |
| 144 | + op.drop_column(from_table, "status") |
| 145 | + |
| 146 | + for t in TABLES_TO_MOVE_STATUS: |
| 147 | + _move_status(op, t["table"], t["column"], "activity", {}) |
| 148 | + postgresql.ENUM(*t["enum"]["values"], name=t["enum"]["name"]).drop(op.get_bind()) |
| 149 | + |
| 150 | + |
| 151 | +def _drop_table_statuses(op): |
| 152 | + for t in TABLES_TO_DROP_STATUS: |
| 153 | + op.drop_column(t["table"], t["column"]) |
| 154 | + for t in TABLES_TO_DROP_STATUS: |
| 155 | + enum = postgresql.ENUM(*t["enum"]["values"], name=t["enum"]["name"]) |
| 156 | + enum.drop(op.get_bind(), checkfirst=True) |
| 157 | + |
| 158 | + |
| 159 | +def upgrade() -> None: |
| 160 | + activity_enum = _create_activity_status_column(op) |
| 161 | + _move_table_statuses(op) |
| 162 | + _drop_table_statuses(op) |
| 163 | + |
| 164 | + conn = op.get_bind() |
| 165 | + |
| 166 | + # for the activity tables that did not have a status a "done" default |
| 167 | + # should be set in the acticity table |
| 168 | + for table in TABLES_WITHOUT_STATUS: |
| 169 | + count = conn.execute( |
| 170 | + text(f""" |
| 171 | + SELECT COUNT(*) |
| 172 | + FROM {table} t |
| 173 | + JOIN activity a ON a.id = t.id |
| 174 | + WHERE a.status != 'done' |
| 175 | + """) |
| 176 | + ).scalar() |
| 177 | + |
| 178 | + if count: |
| 179 | + raise RuntimeError(f"{table}: {count} rows have activity.status != 'done'") |
| 180 | + |
| 181 | + |
| 182 | +def _create_table_enums(conn): |
| 183 | + """Create all old enums.""" |
| 184 | + enums = {} |
| 185 | + for t in TABLES_TO_MOVE_STATUS: |
| 186 | + table_enum = postgresql.ENUM(*t["enum"]["values"], name=t["enum"]["name"]) |
| 187 | + table_enum.create(conn, checkfirst=True) |
| 188 | + enums[t["enum"]["name"]] = table_enum |
| 189 | + for t in TABLES_TO_DROP_STATUS: |
| 190 | + table_enum = postgresql.ENUM(*t["enum"]["values"], name=t["enum"]["name"]) |
| 191 | + table_enum.create(conn, checkfirst=True) |
| 192 | + enums[t["enum"]["name"]] = table_enum |
| 193 | + return enums |
| 194 | + |
| 195 | + |
| 196 | +def _create_table_columns(enums): |
| 197 | + for t in TABLES_TO_MOVE_STATUS: |
| 198 | + op.add_column( |
| 199 | + t["table"], |
| 200 | + sa.Column(t["column"], enums[t["enum"]["name"]], nullable=True), |
| 201 | + ) |
| 202 | + for t in TABLES_TO_DROP_STATUS: |
| 203 | + op.add_column( |
| 204 | + t["table"], |
| 205 | + sa.Column( |
| 206 | + t["column"], |
| 207 | + enums[t["enum"]["name"]], |
| 208 | + nullable=False, |
| 209 | + server_default=t["enum"]["default"], |
| 210 | + ), |
| 211 | + ) |
| 212 | + |
| 213 | + |
| 214 | +def _move_status_from_activity_to_tables(conn): |
| 215 | + for t in TABLES_TO_MOVE_STATUS: |
| 216 | + table_name = t["table"] |
| 217 | + enum_name = t["enum"]["name"] |
| 218 | + conn.execute( |
| 219 | + text(f""" |
| 220 | + UPDATE {table_name} t |
| 221 | + SET status = a.status::text::{enum_name} |
| 222 | + FROM activity a |
| 223 | + WHERE a.id = t.id |
| 224 | + """) |
| 225 | + ) |
| 226 | + |
| 227 | + |
| 228 | +def downgrade() -> None: |
| 229 | + conn = op.get_bind() |
| 230 | + |
| 231 | + enums = _create_table_enums(conn) |
| 232 | + |
| 233 | + # create nullable status columns for tables |
| 234 | + _create_table_columns(enums) |
| 235 | + |
| 236 | + # copy values from activity to tables |
| 237 | + _move_status_from_activity_to_tables(conn) |
| 238 | + |
| 239 | + # remove nullability |
| 240 | + for t in TABLES_TO_MOVE_STATUS: |
| 241 | + op.alter_column( |
| 242 | + t["table"], |
| 243 | + t["column"], |
| 244 | + existing_type=enums[t["enum"]["name"]], |
| 245 | + nullable=False, |
| 246 | + ) |
| 247 | + |
| 248 | + op.drop_column("activity", "status") |
| 249 | + _activity_enum().drop(conn) |
0 commit comments