"""Dynamic pipelines [af27025fe19c].

Revision ID: af27025fe19c
Revises: 0.91.0
Create Date: 2025-10-27 13:23:40.442485

"""

import sqlalchemy as sa
import sqlmodel
from alembic import op

# revision identifiers, used by Alembic.
revision = "af27025fe19c"
down_revision = "0.91.0"
branch_labels = None
depends_on = None


def upgrade() -> None:
    """Upgrade database schema and/or data, creating a new revision."""
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("pipeline_snapshot", schema=None) as batch_op:
        batch_op.add_column(
            sa.Column("is_dynamic", sa.Boolean(), nullable=True)
        )

    op.execute(
        "UPDATE pipeline_snapshot SET is_dynamic = FALSE WHERE is_dynamic IS NULL"
    )
    with op.batch_alter_table("pipeline_snapshot", schema=None) as batch_op:
        batch_op.alter_column(
            "is_dynamic", existing_type=sa.Boolean(), nullable=False
        )

    with op.batch_alter_table("step_configuration", schema=None) as batch_op:
        batch_op.add_column(
            sa.Column(
                "step_run_id", sqlmodel.sql.sqltypes.GUID(), nullable=True
            )
        )
        batch_op.alter_column(
            "snapshot_id", existing_type=sa.CHAR(length=32), nullable=True
        )
        batch_op.create_unique_constraint(
            "unique_step_configuration_for_snapshot_or_step_run",
            ["snapshot_id", "step_run_id", "name"],
        )
        batch_op.drop_constraint(
            "unique_step_name_for_snapshot", type_="unique"
        )
        batch_op.create_foreign_key(
            "fk_step_configuration_step_run_id_step_run",
            "step_run",
            ["step_run_id"],
            ["id"],
            ondelete="CASCADE",
        )

        batch_op.create_check_constraint(
            "ck_step_configuration_snapshot_step_run_exclusivity",
            "((snapshot_id IS NULL AND step_run_id IS NOT NULL) OR "
            "(snapshot_id IS NOT NULL AND step_run_id IS NULL))",
        )

    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade database schema and/or data back to the previous revision."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(
        "ck_step_configuration_snapshot_step_run_exclusivity",
        "step_configuration",
        type_="check",
    )
    with op.batch_alter_table("step_configuration", schema=None) as batch_op:
        batch_op.drop_constraint(
            "fk_step_configuration_step_run_id_step_run", type_="foreignkey"
        )
        batch_op.drop_constraint(
            "unique_step_name_for_snapshot", type_="unique"
        )
        batch_op.create_unique_constraint(
            "unique_step_name_for_snapshot", ["snapshot_id", "name"]
        )
        batch_op.alter_column(
            "snapshot_id", existing_type=sa.CHAR(length=32), nullable=False
        )
        batch_op.drop_column("step_run_id")

    with op.batch_alter_table("pipeline_snapshot", schema=None) as batch_op:
        batch_op.drop_column("is_dynamic")

    # ### end Alembic commands ###
