"""Fence approved stable serving transitions and rollback against ABA changes.""" import sqlalchemy as sa from alembic import op revision = "20260910_0034" down_revision = "20260909_0033" branch_labels = None depends_on = None def upgrade() -> None: op.add_column("capability_contracts", sa.Column( "serving_generation", sa.BigInteger(), nullable=False, server_default="0")) with op.batch_alter_table("capability_contracts") as batch: batch.create_check_constraint("ck_capability_serving_generation", "serving_generation >= 0") def downgrade() -> None: contracts = sa.table("capability_contracts", sa.column("serving_generation")) if op.get_bind().scalar(sa.select(sa.func.count()).select_from(contracts).where( contracts.c.serving_generation != 0 )): raise RuntimeError("Serving route history exists; restore the pre-migration backup instead.") with op.batch_alter_table("capability_contracts") as batch: batch.drop_constraint("ck_capability_serving_generation", type_="check") batch.drop_column("serving_generation")