|
| 1 | +"""add conditions to simulation |
| 2 | +
|
| 3 | +Revision ID: 9b4e2c1a7d50 |
| 4 | +Revises: 01aaa143d690 |
| 5 | +Create Date: 2026-05-06 00:00:00.000000 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import json |
| 10 | +from typing import Any, Sequence, Union |
| 11 | + |
| 12 | +import sqlalchemy as sa |
| 13 | +from alembic import op |
| 14 | + |
| 15 | + |
| 16 | +revision: str = "9b4e2c1a7d50" |
| 17 | +down_revision: Union[str, Sequence[str], None] = "01aaa143d690" |
| 18 | +branch_labels: Union[str, Sequence[str], None] = None |
| 19 | +depends_on: Union[str, Sequence[str], None] = None |
| 20 | + |
| 21 | + |
| 22 | +_CONDITION_KEYS = ("temperature", "pressure") |
| 23 | + |
| 24 | + |
| 25 | +def _as_dict(value: Any) -> dict: |
| 26 | + if value is None: |
| 27 | + return {} |
| 28 | + if isinstance(value, str): |
| 29 | + return json.loads(value or "{}") |
| 30 | + return dict(value) |
| 31 | + |
| 32 | + |
| 33 | +def upgrade() -> None: |
| 34 | + op.add_column( |
| 35 | + "simulations", |
| 36 | + sa.Column( |
| 37 | + "conditions", |
| 38 | + sa.JSON(), |
| 39 | + nullable=False, |
| 40 | + server_default=sa.text("'{}'"), |
| 41 | + ), |
| 42 | + ) |
| 43 | + |
| 44 | + # Backfill: lift temperature/pressure from each model_input's parameters |
| 45 | + # up to the parent simulation's new conditions column. |
| 46 | + bind = op.get_bind() |
| 47 | + simulations = sa.table( |
| 48 | + "simulations", |
| 49 | + sa.column("id", sa.Uuid()), |
| 50 | + sa.column("conditions", sa.JSON()), |
| 51 | + ) |
| 52 | + model_inputs = sa.table( |
| 53 | + "model_inputs", |
| 54 | + sa.column("id", sa.Uuid()), |
| 55 | + sa.column("simulation_id", sa.Uuid()), |
| 56 | + sa.column("parameters", sa.JSON()), |
| 57 | + ) |
| 58 | + |
| 59 | + rows = bind.execute( |
| 60 | + sa.select( |
| 61 | + model_inputs.c.id, model_inputs.c.simulation_id, model_inputs.c.parameters |
| 62 | + ) |
| 63 | + ).all() |
| 64 | + |
| 65 | + sim_conditions: dict = {} |
| 66 | + for mi_id, sim_id, params in rows: |
| 67 | + params = _as_dict(params) |
| 68 | + extracted = {k: params.pop(k) for k in _CONDITION_KEYS if k in params} |
| 69 | + if extracted: |
| 70 | + # Last writer wins if model_inputs disagree; they should match in practice. |
| 71 | + sim_conditions.setdefault(sim_id, {}).update(extracted) |
| 72 | + bind.execute( |
| 73 | + sa.update(model_inputs) |
| 74 | + .where(model_inputs.c.id == mi_id) |
| 75 | + .values(parameters=params) |
| 76 | + ) |
| 77 | + |
| 78 | + for sim_id, conditions in sim_conditions.items(): |
| 79 | + bind.execute( |
| 80 | + sa.update(simulations) |
| 81 | + .where(simulations.c.id == sim_id) |
| 82 | + .values(conditions=conditions) |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +def downgrade() -> None: |
| 87 | + # Push conditions back into each model_input's parameters before dropping the column. |
| 88 | + bind = op.get_bind() |
| 89 | + simulations = sa.table( |
| 90 | + "simulations", |
| 91 | + sa.column("id", sa.Uuid()), |
| 92 | + sa.column("conditions", sa.JSON()), |
| 93 | + ) |
| 94 | + model_inputs = sa.table( |
| 95 | + "model_inputs", |
| 96 | + sa.column("id", sa.Uuid()), |
| 97 | + sa.column("simulation_id", sa.Uuid()), |
| 98 | + sa.column("parameters", sa.JSON()), |
| 99 | + ) |
| 100 | + |
| 101 | + sim_rows = bind.execute(sa.select(simulations.c.id, simulations.c.conditions)).all() |
| 102 | + for sim_id, conditions in sim_rows: |
| 103 | + conditions = _as_dict(conditions) |
| 104 | + if not conditions: |
| 105 | + continue |
| 106 | + mi_rows = bind.execute( |
| 107 | + sa.select(model_inputs.c.id, model_inputs.c.parameters).where( |
| 108 | + model_inputs.c.simulation_id == sim_id |
| 109 | + ) |
| 110 | + ).all() |
| 111 | + for mi_id, params in mi_rows: |
| 112 | + merged = {**conditions, **_as_dict(params)} |
| 113 | + bind.execute( |
| 114 | + sa.update(model_inputs) |
| 115 | + .where(model_inputs.c.id == mi_id) |
| 116 | + .values(parameters=merged) |
| 117 | + ) |
| 118 | + |
| 119 | + op.drop_column("simulations", "conditions") |
0 commit comments