"""seed schools

Revision ID: 8d04b7943264
Revises: <PASSWORD>
Create Date: 2022-04-18 00:38:38.618682+00:00

"""
import json
from os import getcwd
from pathlib import Path

import sqlalchemy as sa
import sqlalchemy.sql as sql
import sqlmodel
from alembic import context, op

# revision identifiers, used by Alembic.
revision = "8d04b7943264"
down_revision = "0<PASSWORD>"
branch_labels = None
depends_on = None

# Ad-hoc schools table for bulk import
schools_table = sql.table(
    "schools", sql.column("id", sa.String), sql.column("name", sa.String)
)


def load_schools():
    migrations_dir = Path(getcwd(), context.script.dir)
    schools_path = migrations_dir.joinpath("schools.json")
    return json.load(open(schools_path, "r"))


def upgrade():
    # Change schools.id to a string
    op.drop_constraint(
        "applications_school_id_fkey", "applications", type_="foreignkey"
    )
    op.alter_column(
        "applications",
        "school_id",
        type_=sqlmodel.sql.sqltypes.AutoString(),
        nullable=False,
    )
    op.alter_column(
        "schools", "id", type_=sqlmodel.sql.sqltypes.AutoString(), nullable=False
    )
    op.create_foreign_key(
        None, "applications", "schools", ["school_id"], ["id"], ondelete="CASCADE"
    )

    # Insert stuff
    schools = load_schools()
    op.bulk_insert(schools_table, [{"id": s["id"], "name": s["name"]} for s in schools])


def downgrade():
    # Delete added records
    schools = load_schools()
    for school in schools:
        op.execute(
            schools_table.delete().where(
                schools_table.c.id == op.inline_literal(school["id"])
            )
        )

    # Change schools.id back to an integer
    op.drop_constraint(
        "applications_school_id_fkey", "applications", type_="foreignkey"
    )
    op.alter_column(
        "applications",
        "school_id",
        type_=sa.Integer(),
        nullable=False,
        postgresql_using="school_id::integer",
    )
    op.alter_column(
        "schools",
        "id",
        type_=sa.Integer(),
        nullable=False,
        postgresql_using="id::integer",
    )
    op.create_foreign_key(
        None, "applications", "schools", ["school_id"], ["id"], ondelete="CASCADE"
    )

alembic.op.create_foreign_key alembic.op.drop_constraint sqlalchemy.Integer os.getcwd sqlalchemy.sql.column alembic.op.bulk_insert sqlmodel.sql.sqltypes.AutoString alembic.op.inline_literal
[((494, 521), 'sqlalchemy.sql.column', 'sql.column', (['"""id"""', 'sa.String'], {}), "('id', sa.String)\n", (504, 521), True, 'import sqlalchemy.sql as sql\n'), ((523, 552), 'sqlalchemy.sql.column', 'sql.column', (['"""name"""', 'sa.String'], {}), "('name', sa.String)\n", (533, 552), True, 'import sqlalchemy.sql as sql\n'), ((795, 885), 'alembic.op.drop_constraint', 'op.drop_constraint', (['"""applications_school_id_fkey"""', '"""applications"""'], {'type_': '"""foreignkey"""'}), "('applications_school_id_fkey', 'applications', type_=\n    'foreignkey')\n", (813, 885), False, 'from alembic import context, op\n'), ((1154, 1256), 'alembic.op.create_foreign_key', 'op.create_foreign_key', (['None', '"""applications"""', '"""schools"""', "['school_id']", "['id']"], {'ondelete': '"""CASCADE"""'}), "(None, 'applications', 'schools', ['school_id'], ['id'\n    ], ondelete='CASCADE')\n", (1175, 1256), False, 'from alembic import context, op\n'), ((1319, 1407), 'alembic.op.bulk_insert', 'op.bulk_insert', (['schools_table', "[{'id': s['id'], 'name': s['name']} for s in schools]"], {}), "(schools_table, [{'id': s['id'], 'name': s['name']} for s in\n    schools])\n", (1333, 1407), False, 'from alembic import context, op\n'), ((1710, 1800), 'alembic.op.drop_constraint', 'op.drop_constraint', (['"""applications_school_id_fkey"""', '"""applications"""'], {'type_': '"""foreignkey"""'}), "('applications_school_id_fkey', 'applications', type_=\n    'foreignkey')\n", (1728, 1800), False, 'from alembic import context, op\n'), ((2137, 2239), 'alembic.op.create_foreign_key', 'op.create_foreign_key', (['None', '"""applications"""', '"""schools"""', "['school_id']", "['id']"], {'ondelete': '"""CASCADE"""'}), "(None, 'applications', 'schools', ['school_id'], ['id'\n    ], ondelete='CASCADE')\n", (2158, 2239), False, 'from alembic import context, op\n'), ((603, 611), 'os.getcwd', 'getcwd', ([], {}), '()\n', (609, 611), False, 'from os import getcwd\n'), ((975, 1009), 'sqlmodel.sql.sqltypes.AutoString', 'sqlmodel.sql.sqltypes.AutoString', ([], {}), '()\n', (1007, 1009), False, 'import sqlmodel\n'), ((1093, 1127), 'sqlmodel.sql.sqltypes.AutoString', 'sqlmodel.sql.sqltypes.AutoString', ([], {}), '()\n', (1125, 1127), False, 'import sqlmodel\n'), ((1890, 1902), 'sqlalchemy.Integer', 'sa.Integer', ([], {}), '()\n', (1900, 1902), True, 'import sqlalchemy as sa\n'), ((2049, 2061), 'sqlalchemy.Integer', 'sa.Integer', ([], {}), '()\n', (2059, 2061), True, 'import sqlalchemy as sa\n'), ((1606, 1637), 'alembic.op.inline_literal', 'op.inline_literal', (["school['id']"], {}), "(school['id'])\n", (1623, 1637), False, 'from alembic import context, op\n')]