|
| 1 | +"""Split up step configurations [3d7e39f3ac92]. |
| 2 | +
|
| 3 | +Revision ID: 3d7e39f3ac92 |
| 4 | +Revises: 0.83.0 |
| 5 | +Create Date: 2025-06-17 17:45:31.702617 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import json |
| 10 | +import uuid |
| 11 | + |
| 12 | +import sqlalchemy as sa |
| 13 | +import sqlmodel |
| 14 | +from alembic import op |
| 15 | +from sqlalchemy.dialects import mysql |
| 16 | + |
| 17 | +from zenml.utils.time_utils import utc_now |
| 18 | + |
| 19 | +# revision identifiers, used by Alembic. |
| 20 | +revision = "3d7e39f3ac92" |
| 21 | +down_revision = "0.83.0" |
| 22 | +branch_labels = None |
| 23 | +depends_on = None |
| 24 | + |
| 25 | + |
| 26 | +def upgrade() -> None: |
| 27 | + """Upgrade database schema and/or data, creating a new revision.""" |
| 28 | + # ### commands auto generated by Alembic - please adjust! ### |
| 29 | + op.create_table( |
| 30 | + "step_configuration", |
| 31 | + sa.Column("id", sqlmodel.sql.sqltypes.GUID(), nullable=False), |
| 32 | + sa.Column("created", sa.DateTime(), nullable=False), |
| 33 | + sa.Column("updated", sa.DateTime(), nullable=False), |
| 34 | + sa.Column("index", sa.Integer(), nullable=False), |
| 35 | + sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False), |
| 36 | + sa.Column( |
| 37 | + "config", |
| 38 | + sa.String(length=16777215).with_variant(mysql.MEDIUMTEXT, "mysql"), |
| 39 | + nullable=False, |
| 40 | + ), |
| 41 | + sa.Column( |
| 42 | + "deployment_id", sqlmodel.sql.sqltypes.GUID(), nullable=False |
| 43 | + ), |
| 44 | + sa.ForeignKeyConstraint( |
| 45 | + ["deployment_id"], |
| 46 | + ["pipeline_deployment.id"], |
| 47 | + name="fk_step_configuration_deployment_id_pipeline_deployment", |
| 48 | + ondelete="CASCADE", |
| 49 | + ), |
| 50 | + sa.PrimaryKeyConstraint("id"), |
| 51 | + sa.UniqueConstraint( |
| 52 | + "deployment_id", "name", name="unique_step_name_for_deployment" |
| 53 | + ), |
| 54 | + ) |
| 55 | + with op.batch_alter_table("pipeline_deployment", schema=None) as batch_op: |
| 56 | + batch_op.add_column( |
| 57 | + sa.Column("step_count", sa.Integer(), nullable=True) |
| 58 | + ) |
| 59 | + |
| 60 | + # Migrate existing step configurations |
| 61 | + connection = op.get_bind() |
| 62 | + meta = sa.MetaData() |
| 63 | + meta.reflect( |
| 64 | + bind=connection, only=("pipeline_deployment", "step_configuration") |
| 65 | + ) |
| 66 | + pipeline_deployment_table = sa.Table("pipeline_deployment", meta) |
| 67 | + step_configuration_table = sa.Table("step_configuration", meta) |
| 68 | + |
| 69 | + step_configurations_to_insert = [] |
| 70 | + deployment_updates = [] |
| 71 | + |
| 72 | + for deployment_id, step_configurations_json in connection.execute( |
| 73 | + sa.select( |
| 74 | + pipeline_deployment_table.c.id, |
| 75 | + pipeline_deployment_table.c.step_configurations, |
| 76 | + ) |
| 77 | + ): |
| 78 | + step_configurations = json.loads(step_configurations_json) |
| 79 | + |
| 80 | + step_count = len(step_configurations) |
| 81 | + deployment_updates.append( |
| 82 | + { |
| 83 | + "id_": deployment_id, |
| 84 | + "step_count": step_count, |
| 85 | + } |
| 86 | + ) |
| 87 | + |
| 88 | + for index, (step_name, step_config) in enumerate( |
| 89 | + step_configurations.items() |
| 90 | + ): |
| 91 | + now = utc_now() |
| 92 | + step_configurations_to_insert.append( |
| 93 | + { |
| 94 | + "id": str(uuid.uuid4()).replace("-", ""), |
| 95 | + "created": now, |
| 96 | + "updated": now, |
| 97 | + "index": index, |
| 98 | + "name": step_name, |
| 99 | + "config": json.dumps(step_config), |
| 100 | + "deployment_id": deployment_id, |
| 101 | + } |
| 102 | + ) |
| 103 | + |
| 104 | + op.bulk_insert( |
| 105 | + step_configuration_table, rows=step_configurations_to_insert |
| 106 | + ) |
| 107 | + if deployment_updates: |
| 108 | + connection.execute( |
| 109 | + sa.update(pipeline_deployment_table).where( |
| 110 | + pipeline_deployment_table.c.id == sa.bindparam("id_") |
| 111 | + ), |
| 112 | + deployment_updates, |
| 113 | + ) |
| 114 | + |
| 115 | + with op.batch_alter_table("pipeline_deployment", schema=None) as batch_op: |
| 116 | + batch_op.alter_column( |
| 117 | + "step_count", existing_type=sa.Integer(), nullable=False |
| 118 | + ) |
| 119 | + batch_op.drop_column("step_configurations") |
| 120 | + |
| 121 | + # ### end Alembic commands ### |
| 122 | + |
| 123 | + |
| 124 | +def downgrade() -> None: |
| 125 | + """Downgrade database schema and/or data back to the previous revision.""" |
| 126 | + # ### commands auto generated by Alembic - please adjust! ### |
| 127 | + with op.batch_alter_table("pipeline_deployment", schema=None) as batch_op: |
| 128 | + batch_op.add_column( |
| 129 | + sa.Column( |
| 130 | + "step_configurations", |
| 131 | + sa.VARCHAR(length=16777215), |
| 132 | + nullable=False, |
| 133 | + ) |
| 134 | + ) |
| 135 | + batch_op.drop_column("step_count") |
| 136 | + |
| 137 | + op.drop_table("step_configuration") |
| 138 | + # ### end Alembic commands ### |
0 commit comments