"""Update tasks
Revision ID: 0002
Revises: 0001
Create Date: 2022-11-02 10:33:34.403970
"""

import sqlalchemy as sa
from alembic import op

import scheduler

# revision identifiers, used by Alembic.
revision = "0002"
down_revision = "0001"
branch_labels = None
depends_on = None


def upgrade():
    bind = op.get_context().bind
    insp = sa.inspect(bind)
    if not insp.has_table("items"):
        # ### commands auto generated by Alembic - please adjust! ###
        op.create_table(
            "items",
            sa.Column("id", scheduler.utils.datastore.GUID(), nullable=False),
            sa.Column("scheduler_id", sa.String(), nullable=True),
            sa.Column("hash", sa.String(), nullable=True),
            sa.Column("priority", sa.Integer(), nullable=True),
            sa.Column("data", sa.JSON(), nullable=False),
            sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
            sa.Column("modified_at", sa.DateTime(timezone=True), nullable=False),
            sa.PrimaryKeyConstraint("id"),
        )

        with op.batch_alter_table("tasks", schema=None) as batch_op:
            batch_op.add_column(sa.Column("p_item", sa.JSON(), nullable=False))
            batch_op.drop_column("task")
            batch_op.drop_column("hash")
        # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("tasks", schema=None) as batch_op:
        batch_op.add_column(sa.Column("hash", sa.VARCHAR(), autoincrement=False, nullable=True))
        batch_op.add_column(sa.Column("task", sa.JSON(), autoincrement=False, nullable=False))
        batch_op.drop_column("p_item")

    op.drop_table("items")
    # ### end Alembic commands ###
