"""INIT

Revision ID: 0be9593348ba
Revises:
Create Date: 2021-01-08 20:02:32.175135

"""
import sqlalchemy as sa
from alembic import op

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


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "anubis_config",
        sa.Column("key", sa.String(length=128), nullable=False),
        sa.Column("value", sa.String(length=2048), nullable=True),
        sa.PrimaryKeyConstraint("key"),
        mysql_charset="utf8mb4",
        mysql_collate="utf8mb4_general_ci",
    )
    op.create_table(
        "course",
        sa.Column("id", sa.String(length=128), nullable=False),
        sa.Column("name", sa.String(length=256), nullable=False),
        sa.Column("course_code", sa.String(length=256), nullable=False),
        sa.Column("semester", sa.String(length=256), nullable=True),
        sa.Column("section", sa.String(length=256), nullable=True),
        sa.Column("professor", sa.String(length=256), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        mysql_charset="utf8mb4",
        mysql_collate="utf8mb4_general_ci",
    )
    op.create_table(
        "static_file",
        sa.Column("id", sa.String(length=128), nullable=False),
        sa.Column("filename", sa.String(length=256), nullable=True),
        sa.Column("path", sa.String(length=256), nullable=True),
        sa.Column("content_type", sa.String(length=128), nullable=True),
        sa.Column("blob", sa.LargeBinary(length=(2 ** 32) - 1), nullable=True),
        sa.Column("hidden", sa.Boolean(), nullable=True),
        sa.Column("created", sa.DateTime(), nullable=True),
        sa.Column("last_updated", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        mysql_charset="utf8mb4",
        mysql_collate="utf8mb4_general_ci",
    )
    op.create_table(
        "user",
        sa.Column("id", sa.String(length=128), nullable=False),
        sa.Column("netid", sa.String(length=128), nullable=False),
        sa.Column("github_username", sa.String(length=128), nullable=True),
        sa.Column("name", sa.String(length=128), nullable=True),
        sa.Column("is_admin", sa.Boolean(), nullable=False),
        sa.Column("is_superuser", sa.Boolean(), nullable=False),
        sa.Column("created", sa.DateTime(), nullable=True),
        sa.Column("last_updated", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id", "netid"),
        mysql_charset="utf8mb4",
        mysql_collate="utf8mb4_general_ci",
    )
    op.create_index(op.f("ix_user_github_username"), "user", ["github_username"], unique=False)
    op.create_index(op.f("ix_user_netid"), "user", ["netid"], unique=True)
    op.create_table(
        "assignment",
        sa.Column("id", sa.String(length=128), nullable=False),
        sa.Column("course_id", sa.String(length=128), nullable=True),
        sa.Column("name", sa.String(length=256), nullable=False),
        sa.Column("hidden", sa.Boolean(), nullable=True),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("github_classroom_url", sa.String(length=256), nullable=True),
        sa.Column("pipeline_image", sa.String(length=256), nullable=True),
        sa.Column("unique_code", sa.String(length=8), nullable=True),
        sa.Column("ide_enabled", sa.Boolean(), nullable=True),
        sa.Column("release_date", sa.DateTime(), nullable=False),
        sa.Column("due_date", sa.DateTime(), nullable=False),
        sa.Column("grace_date", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["course_id"],
            ["course.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name"),
        sa.UniqueConstraint("pipeline_image"),
        sa.UniqueConstraint("unique_code"),
        mysql_charset="utf8mb4",
        mysql_collate="utf8mb4_general_ci",
    )
    op.create_index(op.f("ix_assignment_course_id"), "assignment", ["course_id"], unique=False)
    op.create_table(
        "in_course",
        sa.Column("owner_id", sa.String(length=128), nullable=False),
        sa.Column("course_id", sa.String(length=128), nullable=False),
        sa.ForeignKeyConstraint(
            ["course_id"],
            ["course.id"],
        ),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("owner_id", "course_id"),
        mysql_charset="utf8mb4",
        mysql_collate="utf8mb4_general_ci",
    )
    op.create_table(
        "assignment_question",
        sa.Column("id", sa.String(length=128), nullable=False),
        sa.Column("assignment_id", sa.String(length=128), nullable=True),
        sa.Column("question", sa.Text(), nullable=False),
        sa.Column("solution", sa.Text(), nullable=True),
        sa.Column("sequence", sa.Integer(), nullable=False),
        sa.Column("code_question", sa.Boolean(), nullable=True),
        sa.Column("code_language", sa.String(length=128), nullable=True),
        sa.Column("placeholder", sa.Text(), nullable=True),
        sa.ForeignKeyConstraint(
            ["assignment_id"],
            ["assignment.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        mysql_charset="utf8mb4",
        mysql_collate="utf8mb4_general_ci",
    )
    op.create_index(
        op.f("ix_assignment_question_assignment_id"),
        "assignment_question",
        ["assignment_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_assignment_question_sequence"),
        "assignment_question",
        ["sequence"],
        unique=False,
    )
    op.create_table(
        "assignment_repo",
        sa.Column("id", sa.String(length=128), nullable=False),
        sa.Column("owner_id", sa.String(length=128), nullable=True),
        sa.Column("assignment_id", sa.String(length=128), nullable=False),
        sa.Column("github_username", sa.String(length=256), nullable=False),
        sa.Column("repo_url", sa.String(length=128), nullable=False),
        sa.ForeignKeyConstraint(
            ["assignment_id"],
            ["assignment.id"],
        ),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        mysql_charset="utf8mb4",
        mysql_collate="utf8mb4_general_ci",
    )
    op.create_table(
        "assignment_test",
        sa.Column("id", sa.String(length=128), nullable=False),
        sa.Column("assignment_id", sa.String(length=128), nullable=True),
        sa.Column("name", sa.String(length=128), nullable=True),
        sa.ForeignKeyConstraint(
            ["assignment_id"],
            ["assignment.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        mysql_charset="utf8mb4",
        mysql_collate="utf8mb4_general_ci",
    )
    op.create_index(op.f("ix_assignment_test_name"), "assignment_test", ["name"], unique=False)
    op.create_table(
        "assigned_student_question",
        sa.Column("id", sa.String(length=128), nullable=False),
        sa.Column("response", sa.Text(), nullable=False),
        sa.Column("owner_id", sa.String(length=128), nullable=True),
        sa.Column("assignment_id", sa.String(length=128), nullable=False),
        sa.Column("question_id", sa.String(length=128), nullable=False),
        sa.Column("created", sa.DateTime(), nullable=True),
        sa.Column("last_updated", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["assignment_id"],
            ["assignment.id"],
        ),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["user.id"],
        ),
        sa.ForeignKeyConstraint(
            ["question_id"],
            ["assignment_question.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        mysql_charset="utf8mb4",
        mysql_collate="utf8mb4_general_ci",
    )
    op.create_index(
        op.f("ix_assigned_student_question_assignment_id"),
        "assigned_student_question",
        ["assignment_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_assigned_student_question_question_id"),
        "assigned_student_question",
        ["question_id"],
        unique=False,
    )
    op.create_table(
        "submission",
        sa.Column("id", sa.String(length=128), nullable=False),
        sa.Column("owner_id", sa.String(length=128), nullable=True),
        sa.Column("assignment_id", sa.String(length=128), nullable=False),
        sa.Column("assignment_repo_id", sa.String(length=128), nullable=False),
        sa.Column("created", sa.DateTime(), nullable=True),
        sa.Column("last_updated", sa.DateTime(), nullable=True),
        sa.Column("commit", sa.String(length=128), nullable=False),
        sa.Column("processed", sa.Boolean(), nullable=True),
        sa.Column("state", sa.String(length=128), nullable=True),
        sa.Column("errors", sa.JSON(), nullable=True),
        sa.Column("token", sa.String(length=64), nullable=True),
        sa.ForeignKeyConstraint(
            ["assignment_id"],
            ["assignment.id"],
        ),
        sa.ForeignKeyConstraint(
            ["assignment_repo_id"],
            ["assignment_repo.id"],
        ),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        mysql_charset="utf8mb4",
        mysql_collate="utf8mb4_general_ci",
    )
    op.create_index(
        op.f("ix_submission_assignment_id"),
        "submission",
        ["assignment_id"],
        unique=False,
    )
    op.create_index(op.f("ix_submission_commit"), "submission", ["commit"], unique=True)
    op.create_index(op.f("ix_submission_owner_id"), "submission", ["owner_id"], unique=False)
    op.create_table(
        "theia_session",
        sa.Column("id", sa.String(length=128), nullable=False),
        sa.Column("owner_id", sa.String(length=128), nullable=False),
        sa.Column("assignment_id", sa.String(length=128), nullable=False),
        sa.Column("repo_id", sa.String(length=128), nullable=False),
        sa.Column("active", sa.Boolean(), nullable=True),
        sa.Column("state", sa.String(length=128), nullable=True),
        sa.Column("cluster_address", sa.String(length=256), nullable=True),
        sa.Column("created", sa.DateTime(), nullable=True),
        sa.Column("ended", sa.DateTime(), nullable=True),
        sa.Column("last_heartbeat", sa.DateTime(), nullable=True),
        sa.Column("last_proxy", sa.DateTime(), nullable=True),
        sa.Column("last_updated", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["assignment_id"],
            ["assignment.id"],
        ),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["user.id"],
        ),
        sa.ForeignKeyConstraint(
            ["repo_id"],
            ["assignment_repo.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        mysql_charset="utf8mb4",
        mysql_collate="utf8mb4_general_ci",
    )
    op.create_table(
        "submission_build",
        sa.Column("id", sa.String(length=128), nullable=False),
        sa.Column("submission_id", sa.String(length=128), nullable=True),
        sa.Column("stdout", sa.Text(), nullable=True),
        sa.Column("passed", sa.Boolean(), nullable=True),
        sa.Column("created", sa.DateTime(), nullable=True),
        sa.Column("last_updated", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["submission_id"],
            ["submission.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        mysql_charset="utf8mb4",
        mysql_collate="utf8mb4_general_ci",
    )
    op.create_index(
        op.f("ix_submission_build_submission_id"),
        "submission_build",
        ["submission_id"],
        unique=False,
    )
    op.create_table(
        "submission_test_result",
        sa.Column("id", sa.String(length=128), nullable=False),
        sa.Column("submission_id", sa.String(length=128), nullable=False),
        sa.Column("assignment_test_id", sa.String(length=128), nullable=False),
        sa.Column("created", sa.DateTime(), nullable=True),
        sa.Column("last_updated", sa.DateTime(), nullable=True),
        sa.Column("stdout", sa.Text(), nullable=True),
        sa.Column("message", sa.Text(), nullable=True),
        sa.Column("passed", sa.Boolean(), nullable=True),
        sa.ForeignKeyConstraint(
            ["assignment_test_id"],
            ["assignment_test.id"],
        ),
        sa.ForeignKeyConstraint(
            ["submission_id"],
            ["submission.id"],
        ),
        sa.PrimaryKeyConstraint("id", "submission_id", "assignment_test_id"),
        mysql_charset="utf8mb4",
        mysql_collate="utf8mb4_general_ci",
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("submission_test_result")
    op.drop_index(op.f("ix_submission_build_submission_id"), table_name="submission_build")
    op.drop_table("submission_build")
    op.drop_table("theia_session")
    op.drop_index(op.f("ix_submission_owner_id"), table_name="submission")
    op.drop_index(op.f("ix_submission_commit"), table_name="submission")
    op.drop_index(op.f("ix_submission_assignment_id"), table_name="submission")
    op.drop_table("submission")
    op.drop_index(
        op.f("ix_assigned_student_question_question_id"),
        table_name="assigned_student_question",
    )
    op.drop_index(
        op.f("ix_assigned_student_question_assignment_id"),
        table_name="assigned_student_question",
    )
    op.drop_table("assigned_student_question")
    op.drop_index(op.f("ix_assignment_test_name"), table_name="assignment_test")
    op.drop_table("assignment_test")
    op.drop_table("assignment_repo")
    op.drop_index(op.f("ix_assignment_question_sequence"), table_name="assignment_question")
    op.drop_index(op.f("ix_assignment_question_assignment_id"), table_name="assignment_question")
    op.drop_table("assignment_question")
    op.drop_table("in_course")
    op.drop_index(op.f("ix_assignment_course_id"), table_name="assignment")
    op.drop_table("assignment")
    op.drop_index(op.f("ix_user_netid"), table_name="user")
    op.drop_index(op.f("ix_user_github_username"), table_name="user")
    op.drop_table("user")
    op.drop_table("static_file")
    op.drop_table("course")
    op.drop_table("config")
    # ### end Alembic commands ###
