"""ADD submission id to theia session

Revision ID: 2b1fe6792b7a
Revises: 8834f834adac
Create Date: 2022-10-16 19:05:25.953177

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

# revision identifiers, used by Alembic.
revision = "2b1fe6792b7a"
down_revision = "8834f834adac"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "theia_session",
        sa.Column(
            "submission_id",
            mysql.VARCHAR(
                charset="utf8mb4", collation="utf8mb4_general_ci", length=36
            ),
            nullable=True,
        ),
    )
    op.create_foreign_key(
        None, "theia_session", "submission", ["submission_id"], ["id"]
    )
    conn = op.get_bind()
    with conn.begin():
        conn.execute('UPDATE `theia_session` SET `submission_id` = NULL;')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, "theia_session", type_="foreignkey")
    op.drop_column("theia_session", "submission_id")
    # ### end Alembic commands ###
