"""Add provider_id column to validation_policies

Revision ID: a7b1c2d3e4f5
Revises: 9f8e7d6c5b4a
Create Date: 2026-05-21 10:00:00.000000

"""

from collections.abc import Sequence

from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect

# revision identifiers, used by Alembic.
revision: str = "a7b1c2d3e4f5"
down_revision: str | Sequence[str] | None = "9f8e7d6c5b4a"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def _table_exists(table_name: str) -> bool:
    conn = op.get_bind()
    inspector = inspect(conn)
    return table_name in inspector.get_table_names()


def upgrade() -> None:
    """Add provider_id column to validation_policies as nullable."""
    if not _table_exists("validation_policies"):
        return
    op.add_column(
        "validation_policies",
        sa.Column("provider_id", sa.String(), nullable=True),
    )


def downgrade() -> None:
    """Remove provider_id column from validation_policies."""
    op.drop_column("validation_policies", "provider_id")
