|
| 1 | +"""Unique run index [6e4eb89f632d]. |
| 2 | +
|
| 3 | +Revision ID: 6e4eb89f632d |
| 4 | +Revises: 0.92.0 |
| 5 | +Create Date: 2025-12-03 17:27:32.828004 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Any |
| 10 | + |
| 11 | +import sqlalchemy as sa |
| 12 | +from alembic import op |
| 13 | + |
| 14 | +# revision identifiers, used by Alembic. |
| 15 | +revision = "6e4eb89f632d" |
| 16 | +down_revision = "0.92.0" |
| 17 | +branch_labels = None |
| 18 | +depends_on = None |
| 19 | + |
| 20 | + |
| 21 | +def upgrade() -> None: |
| 22 | + """Upgrade database schema and/or data, creating a new revision.""" |
| 23 | + with op.batch_alter_table("pipeline", schema=None) as batch_op: |
| 24 | + batch_op.add_column( |
| 25 | + sa.Column("run_count", sa.Integer(), nullable=True) |
| 26 | + ) |
| 27 | + |
| 28 | + with op.batch_alter_table("pipeline_run", schema=None) as batch_op: |
| 29 | + batch_op.add_column(sa.Column("index", sa.Integer(), nullable=True)) |
| 30 | + |
| 31 | + connection = op.get_bind() |
| 32 | + meta = sa.MetaData() |
| 33 | + meta.reflect(bind=connection, only=("pipeline_run", "pipeline")) |
| 34 | + run_table = sa.Table("pipeline_run", meta) |
| 35 | + pipeline_table = sa.Table("pipeline", meta) |
| 36 | + |
| 37 | + # Some very old runs (version <0.34.0) might not have a pipeline ID in the |
| 38 | + # rare case where the associated pipeline was deleted after the migration |
| 39 | + # with revision `8ad841ad9bfe`. |
| 40 | + connection.execute( |
| 41 | + sa.update(run_table) |
| 42 | + .where(run_table.c.pipeline_id.is_(None)) |
| 43 | + .values(index=0) |
| 44 | + ) |
| 45 | + |
| 46 | + result = connection.execute( |
| 47 | + sa.select( |
| 48 | + run_table.c.id, |
| 49 | + run_table.c.pipeline_id, |
| 50 | + run_table.c.created, |
| 51 | + ) |
| 52 | + .where(run_table.c.pipeline_id.is_not(None)) |
| 53 | + .order_by(run_table.c.pipeline_id, run_table.c.created, run_table.c.id) |
| 54 | + ).fetchall() |
| 55 | + |
| 56 | + current_pipeline_id = None |
| 57 | + index_within_pipeline = 0 |
| 58 | + run_updates: list[dict[str, Any]] = [] |
| 59 | + run_counts: dict[str, int] = {} |
| 60 | + for row in result: |
| 61 | + pipeline_id = row.pipeline_id |
| 62 | + if pipeline_id != current_pipeline_id: |
| 63 | + current_pipeline_id = pipeline_id |
| 64 | + index_within_pipeline = 1 |
| 65 | + else: |
| 66 | + index_within_pipeline += 1 |
| 67 | + run_updates.append({"id_": row.id, "index": index_within_pipeline}) |
| 68 | + run_counts[pipeline_id] = index_within_pipeline |
| 69 | + |
| 70 | + if run_updates: |
| 71 | + connection.execute( |
| 72 | + sa.update(run_table) |
| 73 | + .where(run_table.c.id == sa.bindparam("id_")) |
| 74 | + .values(index=sa.bindparam("index")), |
| 75 | + run_updates, |
| 76 | + ) |
| 77 | + |
| 78 | + if run_counts: |
| 79 | + pipeline_updates = [ |
| 80 | + {"id_": pipeline_id, "run_count": run_count} |
| 81 | + for pipeline_id, run_count in run_counts.items() |
| 82 | + ] |
| 83 | + connection.execute( |
| 84 | + sa.update(pipeline_table) |
| 85 | + .where(pipeline_table.c.id == sa.bindparam("id_")) |
| 86 | + .values(run_count=sa.bindparam("run_count")), |
| 87 | + pipeline_updates, |
| 88 | + ) |
| 89 | + |
| 90 | + # Step 3: Make columns non-nullable |
| 91 | + with op.batch_alter_table("pipeline_run", schema=None) as batch_op: |
| 92 | + batch_op.alter_column( |
| 93 | + "index", existing_type=sa.Integer(), nullable=False |
| 94 | + ) |
| 95 | + |
| 96 | + with op.batch_alter_table("pipeline", schema=None) as batch_op: |
| 97 | + batch_op.alter_column( |
| 98 | + "run_count", existing_type=sa.Integer(), nullable=False |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +def downgrade() -> None: |
| 103 | + """Downgrade database schema and/or data back to the previous revision.""" |
| 104 | + with op.batch_alter_table("pipeline_run", schema=None) as batch_op: |
| 105 | + batch_op.drop_column("number") |
| 106 | + |
| 107 | + with op.batch_alter_table("pipeline", schema=None) as batch_op: |
| 108 | + batch_op.drop_column("run_count") |
0 commit comments