"""Add inventory_scope to ansible_runs; make inventory_path nullable; backfill scope Revision ID: 0017_ansible_run_scope Revises: 0016_ansible_inventory_groups Create Date: 2026-06-05 """ from typing import Sequence, Union from alembic import op import sqlalchemy as sa revision: str = "0017_ansible_run_scope" down_revision: Union[str, None] = "0016_ansible_inventory_groups" branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: op.add_column( "ansible_runs", sa.Column("inventory_scope", sa.String(255), nullable=True), ) # Backfill: existing rows used repo inventory files — reconstruct the legacy scope string. op.execute( """ UPDATE ansible_runs SET inventory_scope = 'repo:' || source_name || ':' || inventory_path WHERE inventory_path IS NOT NULL AND inventory_path != '' """ ) # Rows with empty inventory_path get 'steward:all' op.execute( "UPDATE ansible_runs SET inventory_scope = 'steward:all' WHERE inventory_scope IS NULL" ) op.alter_column("ansible_runs", "inventory_scope", nullable=False) # Make inventory_path nullable — new steward:* runs won't have a file path. op.alter_column("ansible_runs", "inventory_path", nullable=True) def downgrade() -> None: op.drop_column("ansible_runs", "inventory_scope") op.alter_column("ansible_runs", "inventory_path", nullable=False)