"""Drop OutputOOI

Revision ID: 0003
Revises: 5363940815bb
Create Date: 2021-12-15 10:50:52.581056

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "5363940815bb"
down_revision = "541ec7b3d24e"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###

    # Remove the output_ooi table as Octopoes now contains Combinations for deletion propagation
    op.drop_table("output_ooi")

    # Set a CASCADE on the boefje_meta_id foreign key in normalizer_meta
    op.drop_constraint("normalizer_meta_boefje_meta_id_fkey", "normalizer_meta", type_="foreignkey")
    op.create_foreign_key(
        "normalizer_meta_boefje_meta_id_fkey",
        "normalizer_meta",
        "boefje_meta",
        ["boefje_meta_id"],
        ["id"],
        ondelete="CASCADE",
    )

    # Clear the boefje_meta table, so we can make the input_ooi column not nullable
    op.execute("DELETE FROM boefje_meta")
    op.add_column("boefje_meta", sa.Column("input_ooi", sa.String(), nullable=False))

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("boefje_meta", "input_ooi")

    # Drop the CASCADE on the boefje_meta_id foreign key in normalizer_meta
    op.drop_constraint("normalizer_meta_boefje_meta_id_fkey", "normalizer_meta", type_="foreignkey")
    op.create_foreign_key(
        "normalizer_meta_boefje_meta_id_fkey", "normalizer_meta", "boefje_meta", ["boefje_meta_id"], ["id"]
    )
    op.create_table(
        "output_ooi",
        sa.Column("ooi_id", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("normalizer_meta_id", postgresql.UUID(), autoincrement=False, nullable=False),
        sa.ForeignKeyConstraint(
            ["normalizer_meta_id"], ["normalizer_meta.id"], name="output_ooi_normalizer_meta_id_fkey"
        ),
        sa.PrimaryKeyConstraint("ooi_id", "normalizer_meta_id", name="output_ooi_pkey"),
    )
    # ### end Alembic commands ###
