"""init

Revision ID: 014da66fd11f
Revises:
Create Date: 2024-08-17 23:06:22.463683

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import sqlmodel  # noqa F401


# revision identifiers, used by Alembic.
revision: str = "014da66fd11f"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "pip_lockfile_cache",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column("hash", sa.TEXT(), nullable=False),
        sa.Column("lockfile", sa.TEXT(), nullable=False),
        sa.Column("expiration_time", sa.TIMESTAMP(), nullable=False),
        sa.PrimaryKeyConstraint("hash"),
    )
    op.create_table(
        "python_actions",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column("user_id", sa.TEXT(), nullable=False),
        sa.Column("action_type", sa.TEXT(), nullable=False),
        sa.Column("import_statements", sa.TEXT(), nullable=False),
        sa.Column("code", sa.TEXT(), nullable=False),
        sa.Column("display_name", sa.TEXT(), nullable=False),
        sa.Column("display_namespace", sa.TEXT(), nullable=False),
        sa.Column("description", sa.TEXT(), nullable=True),
        sa.Column("secrets_placeholders", sa.TEXT(), nullable=True),
        sa.Column("requirements", sa.TEXT(), nullable=True),
        sa.Column("arguments", sa.JSON(), nullable=False),
        sa.PrimaryKeyConstraint("user_id", "action_type"),
    )
    op.create_table(
        "secrets",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column("secret_id", sa.TEXT(), nullable=False),
        sa.Column("user_id", sa.TEXT(), nullable=False),
        sa.Column("encrypted_secret", sa.TEXT(), nullable=True),
        sa.Column("schema_json_serialized", sa.TEXT(), nullable=False),
        sa.PrimaryKeyConstraint("secret_id"),
    )
    op.create_index(op.f("ix_secrets_user_id"), "secrets", ["user_id"], unique=False)
    op.create_table(
        "workflows",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column("workflow_id", sa.TEXT(), nullable=False),
        sa.Column("user_id", sa.TEXT(), nullable=False),
        sa.Column("workflow_name", sa.TEXT(), nullable=False),
        sa.Column("workflow_dag", sa.JSON(), nullable=False),
        sa.Column("is_active", sa.BOOLEAN(), nullable=False),
        sa.PrimaryKeyConstraint("workflow_id"),
        sa.UniqueConstraint("user_id", "workflow_name", name="unique_workflow_name"),
    )
    op.create_index(
        op.f("ix_workflows_user_id"), "workflows", ["user_id"], unique=False
    )
    op.create_index(
        op.f("ix_workflows_workflow_name"), "workflows", ["workflow_name"], unique=False
    )
    op.create_table(
        "workflow_runs",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column("run_id", sa.TEXT(), nullable=False),
        sa.Column("user_id", sa.TEXT(), nullable=False),
        sa.Column("workflow_id", sa.TEXT(), nullable=False),
        sa.Column("source_name", sa.TEXT(), nullable=False),
        sa.ForeignKeyConstraint(
            ["workflow_id"], ["workflows.workflow_id"], ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("run_id"),
    )
    op.create_table(
        "workflow_schedules",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column("schedule_id", sa.TEXT(), nullable=False),
        sa.Column("user_id", sa.TEXT(), nullable=False),
        sa.Column("workflow_id", sa.TEXT(), nullable=False),
        sa.Column("cron", sa.TEXT(), nullable=True),
        sa.Column("interval_seconds", sa.INTEGER(), nullable=True),
        sa.Column("interval_minutes", sa.INTEGER(), nullable=True),
        sa.Column("interval_hours", sa.INTEGER(), nullable=True),
        sa.Column("interval_days", sa.INTEGER(), nullable=True),
        sa.Column("default_args", sa.JSON(), nullable=True),
        sa.ForeignKeyConstraint(
            ["workflow_id"], ["workflows.workflow_id"], ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("schedule_id"),
    )
    op.create_index(
        op.f("ix_workflow_schedules_user_id"),
        "workflow_schedules",
        ["user_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_workflow_schedules_workflow_id"),
        "workflow_schedules",
        ["workflow_id"],
        unique=False,
    )
    op.create_table(
        "workflow_webhooks",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column("webhook_id", sa.TEXT(), nullable=False),
        sa.Column("workflow_id", sa.TEXT(), nullable=False),
        sa.Column("webhook_secret", sa.TEXT(), nullable=False),
        sa.ForeignKeyConstraint(
            ["workflow_id"], ["workflows.workflow_id"], ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("webhook_id"),
    )
    op.create_index(
        op.f("ix_workflow_webhooks_workflow_id"),
        "workflow_webhooks",
        ["workflow_id"],
        unique=False,
    )
    op.create_table(
        "workflow_run_steps",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column("step_id", sa.TEXT(), nullable=False),
        sa.Column("run_id", sa.TEXT(), nullable=False),
        sa.Column("prev_step_id", sa.TEXT(), nullable=True),
        sa.Column("action_type", sa.TEXT(), nullable=False),
        sa.Column("input_args", sa.JSON(), nullable=True),
        sa.Column("logs", sa.TEXT(), nullable=True),
        sa.Column("result", sa.JSON(), nullable=True),
        sa.ForeignKeyConstraint(
            ["run_id"], ["workflow_runs.run_id"], ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("step_id"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("workflow_run_steps")
    op.drop_index(
        op.f("ix_workflow_webhooks_workflow_id"), table_name="workflow_webhooks"
    )
    op.drop_table("workflow_webhooks")
    op.drop_index(
        op.f("ix_workflow_schedules_workflow_id"), table_name="workflow_schedules"
    )
    op.drop_index(
        op.f("ix_workflow_schedules_user_id"), table_name="workflow_schedules"
    )
    op.drop_table("workflow_schedules")
    op.drop_table("workflow_runs")
    op.drop_index(op.f("ix_workflows_workflow_name"), table_name="workflows")
    op.drop_index(op.f("ix_workflows_user_id"), table_name="workflows")
    op.drop_table("workflows")
    op.drop_index(op.f("ix_secrets_user_id"), table_name="secrets")
    op.drop_table("secrets")
    op.drop_table("python_actions")
    op.drop_table("pip_lockfile_cache")
    # ### end Alembic commands ###
