# Copyright (C) 2020 Bloomberg LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#  <http://www.apache.org/licenses/LICENSE-2.0>
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""New database with all earlier migrations squashed

Revision ID: e287a533bbd7
Revises:
Create Date: 2020-02-06 13:36:00.607572

"""

import sqlalchemy as sa
from alembic import op

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


def upgrade() -> None:
    # ### commands auto generated by Alembic ###
    op.create_table(
        "index",
        sa.Column("digest_hash", sa.String(), nullable=False),
        sa.Column("digest_size_bytes", sa.Integer(), nullable=False),
        sa.Column("accessed_timestamp", sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint("digest_hash"),
    )
    op.create_index(op.f("ix_index_accessed_timestamp"), "index", ["accessed_timestamp"], unique=False)
    op.create_index(op.f("ix_index_digest_hash"), "index", ["digest_hash"], unique=False)
    op.create_table(
        "jobs",
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("action_digest", sa.String(), nullable=False),
        sa.Column("priority", sa.Integer(), nullable=False),
        sa.Column("stage", sa.Integer(), nullable=False),
        sa.Column("do_not_cache", sa.Boolean(), nullable=False),
        sa.Column("cancelled", sa.Boolean(), nullable=False),
        sa.Column("queued_timestamp", sa.DateTime(), nullable=True),
        sa.Column("queued_time_duration", sa.Integer(), nullable=True),
        sa.Column("worker_start_timestamp", sa.DateTime(), nullable=True),
        sa.Column("worker_completed_timestamp", sa.DateTime(), nullable=True),
        sa.Column("result", sa.String(), nullable=True),
        sa.Column("assigned", sa.Boolean(), nullable=True),
        sa.Column("n_tries", sa.Integer(), nullable=True),
        sa.Column("platform_requirements", sa.String(), nullable=True),
        sa.PrimaryKeyConstraint("name"),
    )
    op.create_index(op.f("ix_jobs_action_digest"), "jobs", ["action_digest"], unique=False)
    op.create_index(op.f("ix_jobs_priority"), "jobs", ["priority"], unique=False)
    op.create_index(op.f("ix_jobs_stage"), "jobs", ["stage"], unique=False)
    op.create_table(
        "leases",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("job_name", sa.String(), nullable=False),
        sa.Column("status", sa.Integer(), nullable=True),
        sa.Column("state", sa.Integer(), nullable=False),
        sa.Column("worker_name", sa.String(), nullable=True),
        sa.ForeignKeyConstraint(["job_name"], ["jobs.name"], onupdate="CASCADE", ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_leases_job_name"), "leases", ["job_name"], unique=False)
    op.create_index(op.f("ix_leases_worker_name"), "leases", ["worker_name"], unique=False)
    op.create_table(
        "operations",
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("job_name", sa.String(), nullable=False),
        sa.Column("cancelled", sa.Boolean(), nullable=False),
        sa.ForeignKeyConstraint(["job_name"], ["jobs.name"], onupdate="CASCADE", ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("name"),
    )
    op.create_index(op.f("ix_operations_job_name"), "operations", ["job_name"], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic ###
    op.drop_index(op.f("ix_operations_job_name"), table_name="operations")
    op.drop_table("operations")
    op.drop_index(op.f("ix_leases_worker_name"), table_name="leases")
    op.drop_index(op.f("ix_leases_job_name"), table_name="leases")
    op.drop_table("leases")
    op.drop_index(op.f("ix_jobs_stage"), table_name="jobs")
    op.drop_index(op.f("ix_jobs_priority"), table_name="jobs")
    op.drop_index(op.f("ix_jobs_action_digest"), table_name="jobs")
    op.drop_table("jobs")
    op.drop_index(op.f("ix_index_digest_hash"), table_name="index")
    op.drop_index(op.f("ix_index_accessed_timestamp"), table_name="index")
    op.drop_table("index")
    # ### end Alembic commands ###
