"""remove triggers [97109a4a8d26].

Revision ID: 97109a4a8d26
Revises: 8e36b8227160
Create Date: 2026-01-26 21:21:19.364737

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "97109a4a8d26"
down_revision = "0.93.3"
branch_labels = None
depends_on = None


def upgrade() -> None:
    """Upgrade database schema and/or data, creating a new revision."""
    with op.batch_alter_table("pipeline_run", schema=None) as batch_op:
        batch_op.drop_constraint(
            "fk_pipeline_run_trigger_execution_id_trigger_execution",
            type_="foreignkey",
        )
        batch_op.drop_column("trigger_execution_id")

    op.drop_table("trigger_execution")  # Should also drop the trigger fk
    op.drop_table("trigger")  # Should also drop the action, event_source fk
    op.drop_table("event_source")
    op.drop_table("action")


def downgrade() -> None:
    """Downgrade database schema and/or data back to the previous revision."""
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("pipeline_run", schema=None) as batch_op:
        batch_op.add_column(
            sa.Column(
                "trigger_execution_id", sa.CHAR(length=32), nullable=True
            )
        )
        batch_op.create_foreign_key(
            "fk_pipeline_run_trigger_execution_id_trigger_execution",
            "trigger_execution",
            ["trigger_execution_id"],
            ["id"],
            ondelete="SET NULL",
        )

    op.create_table(
        "trigger_execution",
        sa.Column("id", sa.CHAR(length=32), nullable=False),
        sa.Column("created", sa.DATETIME(), nullable=False),
        sa.Column("updated", sa.DATETIME(), nullable=False),
        sa.Column("trigger_id", sa.CHAR(length=32), nullable=False),
        sa.Column("event_metadata", sa.BLOB(), nullable=True),
        sa.ForeignKeyConstraint(
            ["trigger_id"],
            ["trigger.id"],
            name="fk_trigger_execution_trigger_id_trigger",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "trigger",
        sa.Column("id", sa.CHAR(length=32), nullable=False),
        sa.Column("created", sa.DATETIME(), nullable=False),
        sa.Column("updated", sa.DATETIME(), nullable=False),
        sa.Column("name", sa.VARCHAR(), nullable=False),
        sa.Column("project_id", sa.CHAR(length=32), nullable=False),
        sa.Column("user_id", sa.CHAR(length=32), nullable=True),
        sa.Column("event_source_id", sa.CHAR(length=32), nullable=True),
        sa.Column("action_id", sa.CHAR(length=32), nullable=False),
        sa.Column("event_filter", sa.BLOB(), nullable=False),
        sa.Column("schedule", sa.BLOB(), nullable=True),
        sa.Column("description", sa.TEXT(), nullable=True),
        sa.Column("is_active", sa.BOOLEAN(), nullable=False),
        sa.ForeignKeyConstraint(
            ["action_id"],
            ["action.id"],
            name="fk_trigger_action_id_action",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["event_source_id"],
            ["event_source.id"],
            name="fk_trigger_event_source_id_event_source",
            ondelete="SET NULL",
        ),
        sa.ForeignKeyConstraint(
            ["project_id"],
            ["project.id"],
            name="fk_trigger_project_id_project",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
            name="fk_trigger_user_id_user",
            ondelete="SET NULL",
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "name", "project_id", name="unique_trigger_name_in_project"
        ),
    )
    op.create_table(
        "action",
        sa.Column("id", sa.CHAR(length=32), nullable=False),
        sa.Column("created", sa.DATETIME(), nullable=False),
        sa.Column("updated", sa.DATETIME(), nullable=False),
        sa.Column("name", sa.VARCHAR(), nullable=False),
        sa.Column("project_id", sa.CHAR(length=32), nullable=False),
        sa.Column("user_id", sa.CHAR(length=32), nullable=True),
        sa.Column("service_account_id", sa.CHAR(length=32), nullable=False),
        sa.Column("auth_window", sa.INTEGER(), nullable=False),
        sa.Column("flavor", sa.VARCHAR(), nullable=False),
        sa.Column("plugin_subtype", sa.VARCHAR(), nullable=False),
        sa.Column("description", sa.TEXT(), nullable=True),
        sa.Column("configuration", sa.BLOB(), nullable=False),
        sa.ForeignKeyConstraint(
            ["project_id"],
            ["project.id"],
            name="fk_action_project_id_project",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["service_account_id"],
            ["user.id"],
            name="fk_action_service_account_id_user",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
            name="fk_action_user_id_user",
            ondelete="SET NULL",
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "name", "project_id", name="unique_action_name_in_project"
        ),
    )
    op.create_table(
        "event_source",
        sa.Column("id", sa.CHAR(length=32), nullable=False),
        sa.Column("created", sa.DATETIME(), nullable=False),
        sa.Column("updated", sa.DATETIME(), nullable=False),
        sa.Column("name", sa.VARCHAR(), nullable=False),
        sa.Column("project_id", sa.CHAR(length=32), nullable=False),
        sa.Column("user_id", sa.CHAR(length=32), nullable=True),
        sa.Column("flavor", sa.VARCHAR(), nullable=False),
        sa.Column("plugin_subtype", sa.VARCHAR(), nullable=False),
        sa.Column("description", sa.TEXT(), nullable=True),
        sa.Column("configuration", sa.BLOB(), nullable=False),
        sa.Column("is_active", sa.BOOLEAN(), nullable=False),
        sa.ForeignKeyConstraint(
            ["project_id"],
            ["project.id"],
            name="fk_event_source_project_id_project",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
            name="fk_event_source_user_id_user",
            ondelete="SET NULL",
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "name", "project_id", name="unique_event_source_name_in_project"
        ),
    )
    # ### end Alembic commands ###
