|
| 1 | +"""refactor simulation to use model_inputs table |
| 2 | +
|
| 3 | +Revision ID: 01aaa143d690 |
| 4 | +Revises: c35588effcc4 |
| 5 | +Create Date: 2026-01-21 16:50:46.128272 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Sequence, Union |
| 10 | + |
| 11 | +import sqlalchemy as sa |
| 12 | +from alembic import op |
| 13 | +from sqlalchemy.dialects import postgresql |
| 14 | + |
| 15 | +# revision identifiers, used by Alembic. |
| 16 | +revision: str = "01aaa143d690" |
| 17 | +down_revision: Union[str, Sequence[str], None] = "c35588effcc4" |
| 18 | +branch_labels: Union[str, Sequence[str], None] = None |
| 19 | +depends_on: Union[str, Sequence[str], None] = None |
| 20 | + |
| 21 | + |
| 22 | +def upgrade() -> None: |
| 23 | + """Upgrade schema.""" |
| 24 | + # ### commands auto generated by Alembic - please adjust! ### |
| 25 | + op.create_table( |
| 26 | + "model_inputs", |
| 27 | + sa.Column("simulation_id", sa.Uuid(), nullable=False), |
| 28 | + sa.Column("previous_model_input_id", sa.Uuid(), nullable=True), |
| 29 | + sa.Column("model_id", sa.String(), nullable=False), |
| 30 | + sa.Column("parameters", sa.JSON(), nullable=False), |
| 31 | + sa.Column("id", sa.Uuid(), nullable=False), |
| 32 | + sa.Column("created_at", sa.DateTime(), nullable=False), |
| 33 | + sa.Column("updated_at", sa.DateTime(), nullable=False), |
| 34 | + sa.ForeignKeyConstraint( |
| 35 | + ["simulation_id"], |
| 36 | + ["simulations.id"], |
| 37 | + ), |
| 38 | + sa.ForeignKeyConstraint( |
| 39 | + ["previous_model_input_id"], |
| 40 | + ["model_inputs.id"], |
| 41 | + ), |
| 42 | + sa.PrimaryKeyConstraint("id"), |
| 43 | + ) |
| 44 | + op.add_column("results", sa.Column("model_input_id", sa.Uuid(), nullable=True)) |
| 45 | + |
| 46 | + op.execute(""" |
| 47 | + INSERT INTO model_inputs (id, simulation_id, parameters, created_at, updated_at, model_id) |
| 48 | + SELECT |
| 49 | + gen_random_uuid(), |
| 50 | + simulations.id, |
| 51 | + simulations.parameters, |
| 52 | + simulations.created_at, |
| 53 | + simulations.updated_at, |
| 54 | + simulations.model_id |
| 55 | + FROM simulations |
| 56 | + """) |
| 57 | + |
| 58 | + op.execute(""" |
| 59 | + UPDATE results |
| 60 | + SET model_input_id = model_inputs.id |
| 61 | + FROM model_inputs |
| 62 | + WHERE results.simulation_id = model_inputs.simulation_id |
| 63 | + """) |
| 64 | + |
| 65 | + op.alter_column("results", "model_input_id", nullable=False) |
| 66 | + op.drop_constraint( |
| 67 | + op.f("results_simulation_id_fkey"), "results", type_="foreignkey" |
| 68 | + ) |
| 69 | + op.create_foreign_key(None, "results", "model_inputs", ["model_input_id"], ["id"]) |
| 70 | + op.drop_column("results", "simulation_id") |
| 71 | + op.drop_column("simulations", "model_id") |
| 72 | + op.drop_column("simulations", "parameters") |
| 73 | + # ### end Alembic commands ### |
| 74 | + |
| 75 | + |
| 76 | +def downgrade() -> None: |
| 77 | + """Downgrade schema.""" |
| 78 | + # ### commands auto generated by Alembic - please adjust! ### |
| 79 | + op.add_column( |
| 80 | + "simulations", |
| 81 | + sa.Column( |
| 82 | + "parameters", |
| 83 | + postgresql.JSON(astext_type=sa.Text()), |
| 84 | + autoincrement=False, |
| 85 | + nullable=True, |
| 86 | + ), |
| 87 | + ) |
| 88 | + op.add_column( |
| 89 | + "simulations", |
| 90 | + sa.Column("model_id", sa.VARCHAR(), autoincrement=False, nullable=True), |
| 91 | + ) |
| 92 | + op.add_column( |
| 93 | + "results", |
| 94 | + sa.Column("simulation_id", sa.UUID(), autoincrement=False, nullable=True), |
| 95 | + ) |
| 96 | + |
| 97 | + op.execute(""" |
| 98 | + UPDATE simulations |
| 99 | + SET parameters = model_inputs.parameters, |
| 100 | + model_id = model_inputs.model_id |
| 101 | + FROM model_inputs |
| 102 | + WHERE model_inputs.simulation_id = simulations.id |
| 103 | + """) |
| 104 | + |
| 105 | + op.execute(""" |
| 106 | + UPDATE results |
| 107 | + SET simulation_id = model_inputs.simulation_id |
| 108 | + FROM model_inputs |
| 109 | + WHERE results.model_input_id = model_inputs.id |
| 110 | + """) |
| 111 | + |
| 112 | + op.alter_column("simulations", "parameters", nullable=False) |
| 113 | + op.alter_column("simulations", "model_id", nullable=False) |
| 114 | + op.alter_column("results", "simulation_id", nullable=False) |
| 115 | + |
| 116 | + op.create_foreign_key( |
| 117 | + op.f("results_simulation_id_fkey"), |
| 118 | + "results", |
| 119 | + "simulations", |
| 120 | + ["simulation_id"], |
| 121 | + ["id"], |
| 122 | + ) |
| 123 | + op.drop_column("results", "model_input_id") |
| 124 | + op.drop_table("model_inputs") |
| 125 | + # ### end Alembic commands ### |
0 commit comments