"""add build_tasks table and tasks.build_task_id

Revision ID: 1b557dfe7b15
Revises: 11939e54d764
Create Date: 2026-07-01 03:44:50.272808

"""

from collections.abc import Sequence

import sqlalchemy as sa
import sqlmodel
from alembic import op

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


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "build_tasks",
        sa.Column("id", sa.BigInteger(), nullable=False),
        sa.Column("image_key", sa.String(), nullable=False),
        sa.Column("image_tag", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("git_remote", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("git_sha", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("dockerfile", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("status", sa.String(), nullable=False),
        sa.Column("holder_worker_id", sa.BigInteger(), nullable=True),
        sa.Column("lease_expires_at", sa.DateTime(), nullable=True),
        sa.Column("log_path", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("error", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("attempts", sa.Integer(), nullable=False),
        sa.Column("max_retries", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("started_at", sa.DateTime(), nullable=True),
        sa.Column("finished_at", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("image_key"),
    )
    op.create_index(op.f("ix_build_tasks_image_key"), "build_tasks", ["image_key"], unique=False)
    op.create_index(op.f("ix_build_tasks_status"), "build_tasks", ["status"], unique=False)
    op.add_column("tasks", sa.Column("build_task_id", sa.BigInteger(), nullable=True))
    op.create_index(op.f("ix_tasks_build_task_id"), "tasks", ["build_task_id"], unique=False)
    op.create_foreign_key(None, "tasks", "build_tasks", ["build_task_id"], ["id"])
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, "tasks", type_="foreignkey")
    op.drop_index(op.f("ix_tasks_build_task_id"), table_name="tasks")
    op.drop_column("tasks", "build_task_id")
    op.drop_index(op.f("ix_build_tasks_status"), table_name="build_tasks")
    op.drop_index(op.f("ix_build_tasks_image_key"), table_name="build_tasks")
    op.drop_table("build_tasks")
    # ### end Alembic commands ###
