"""UWS schema

Revision ID: e9299566bc19
Revises:
Create Date: 2024-09-11 00:00:57.336783+00:00

"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

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


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "job",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("message_id", sa.String(length=64), nullable=True),
        sa.Column("owner", sa.String(length=64), nullable=False),
        sa.Column(
            "phase",
            sa.Enum(
                "PENDING",
                "QUEUED",
                "EXECUTING",
                "COMPLETED",
                "ERROR",
                "UNKNOWN",
                "HELD",
                "SUSPENDED",
                "ABORTED",
                "ARCHIVED",
                name="executionphase",
            ),
            nullable=False,
        ),
        sa.Column("run_id", sa.String(length=64), nullable=True),
        sa.Column("creation_time", sa.DateTime(), nullable=False),
        sa.Column("start_time", sa.DateTime(), nullable=True),
        sa.Column("end_time", sa.DateTime(), nullable=True),
        sa.Column("destruction_time", sa.DateTime(), nullable=False),
        sa.Column("execution_duration", sa.Integer(), nullable=False),
        sa.Column("quote", sa.DateTime(), nullable=True),
        sa.Column(
            "error_type",
            sa.Enum("TRANSIENT", "FATAL", name="errortype"),
            nullable=True,
        ),
        sa.Column(
            "error_code",
            sa.Enum(
                "AUTHENTICATION_ERROR",
                "AUTHORIZATION_ERROR",
                "MULTIVALUED_PARAM_NOT_SUPPORTED",
                "ERROR",
                "SERVICE_UNAVAILABLE",
                "USAGE_ERROR",
                name="errorcode",
            ),
            nullable=True,
        ),
        sa.Column("error_message", sa.Text(), nullable=True),
        sa.Column("error_detail", sa.Text(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        "by_owner_phase",
        "job",
        ["owner", "phase", "creation_time"],
        unique=False,
    )
    op.create_index(
        "by_owner_time", "job", ["owner", "creation_time"], unique=False
    )
    op.create_table(
        "job_parameter",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("job_id", sa.Integer(), nullable=False),
        sa.Column("parameter", sa.String(length=64), nullable=False),
        sa.Column("value", sa.Text(), nullable=False),
        sa.Column("is_post", sa.Boolean(), nullable=False),
        sa.ForeignKeyConstraint(["job_id"], ["job.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        "by_parameter", "job_parameter", ["job_id", "parameter"], unique=False
    )
    op.create_table(
        "job_result",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("job_id", sa.Integer(), nullable=False),
        sa.Column("result_id", sa.String(length=64), nullable=False),
        sa.Column("sequence", sa.Integer(), nullable=False),
        sa.Column("url", sa.String(length=256), nullable=False),
        sa.Column("size", sa.Integer(), nullable=True),
        sa.Column("mime_type", sa.String(length=64), nullable=True),
        sa.ForeignKeyConstraint(["job_id"], ["job.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        "by_result_id", "job_result", ["job_id", "result_id"], unique=True
    )
    op.create_index(
        "by_sequence", "job_result", ["job_id", "sequence"], unique=True
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index("by_sequence", table_name="job_result")
    op.drop_index("by_result_id", table_name="job_result")
    op.drop_table("job_result")
    op.drop_index("by_parameter", table_name="job_parameter")
    op.drop_table("job_parameter")
    op.drop_index("by_owner_time", table_name="job")
    op.drop_index("by_owner_phase", table_name="job")
    op.drop_table("job")
    # ### end Alembic commands ###
