"""CHG assignment repo netid tracking

Revision ID: 28971042fc9c
Revises: 7a2f58a7654f
Create Date: 2022-01-15 18:21:07.682465

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = "28971042fc9c"
down_revision = "7a2f58a7654f"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "assignment_repo",
        sa.Column("netid", mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=128), nullable=True),
    )

    conn = op.get_bind()
    with conn.begin():
        conn.execute('update assignment_repo '
                     'join user on assignment_repo.owner_id = user.id '
                     'set assignment_repo.netid = user.netid;')
        # Drop dangling
        conn.execute('delete sb from assignment_repo ar '
                     'join submission s on s.assignment_repo_id = ar.id '
                     'join submission_build sb on sb.submission_id = s.id '
                     'where ar.netid is NULL;')
        conn.execute('delete str from assignment_repo ar '
                     'join submission s on s.assignment_repo_id = ar.id '
                     'join submission_test_result str on str.submission_id = s.id '
                     'where ar.netid is NULL;')
        conn.execute('delete s from assignment_repo ar '
                     'join submission s on s.assignment_repo_id = ar.id '
                     'where ar.netid is NULL;')
        conn.execute('delete ar from assignment_repo ar '
                     'where ar.netid is NULL;')

    op.alter_column(
        "assignment_repo",
        "netid",
        existing_type=mysql.VARCHAR(collation="utf8mb4_unicode_ci", length=128),
        nullable=False,
    )

    op.drop_column("assignment_repo", "github_username")
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "assignment_repo",
        sa.Column("github_username", mysql.MEDIUMTEXT(), nullable=False),
    )
    op.drop_column("assignment_repo", "netid")
    # ### end Alembic commands ###
