"""empty message

Revision ID: d749f1e49a9a
Revises: c043b29e085b
Create Date: 2020-07-07 12:31:58.592135

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "d749f1e49a9a"
down_revision = "c043b29e085b"
branch_labels = None
depends_on = None


def upgrade(engine_name):
    globals()["upgrade_%s" % engine_name]()


def downgrade(engine_name):
    globals()["downgrade_%s" % engine_name]()


def upgrade_opennem():
    op.execute(
        "ALTER TABLE wem_facility DROP CONSTRAINT wem_facility_pkey CASCADE"
    )

    op.add_column(
        "wem_facility", sa.Column("id", sa.Integer(), nullable=False)
    )

    # op.drop_constraint(
    #     "fk_facility_scada_facility_id",
    #     "wem_facility_scada",
    #     type_="foreignkey",
    # )

    op.alter_column(
        "wem_facility", "code", existing_type=sa.TEXT(), nullable=True
    )

    op.create_foreign_key(
        "fk_facility_scada_facility_id",
        "wem_facility_scada",
        "wem_facility",
        ["facility_id"],
        ["id"],
    )
    op.drop_column("wem_facility_scada", "quantity")
    # ### end Alembic commands ###


def downgrade_opennem():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "wem_facility_scada",
        sa.Column(
            "quantity", sa.NUMERIC(), autoincrement=False, nullable=True
        ),
    )
    op.drop_constraint(
        "fk_facility_scada_facility_id",
        "wem_facility_scada",
        type_="foreignkey",
    )
    op.create_foreign_key(
        "fk_facility_scada_facility_id",
        "wem_facility_scada",
        "wem_facility",
        ["facility_id"],
        ["code"],
    )
    op.alter_column(
        "wem_facility", "code", existing_type=sa.TEXT(), nullable=False
    )
    op.drop_column("wem_facility", "id")
    # ### end Alembic commands ###
