# pylint: disable=invalid-name
"""Hybrid audit type

Revision ID: 5bb25bdd56b7
Revises: 8b6b89bfbc97
Create Date: 2021-02-17 20:00:17.417808+00:00

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "5bb25bdd56b7"
down_revision = "8b6b89bfbc97"
branch_labels = None
depends_on = None


def upgrade():
    # Alembic doesn't support changing enums, so we have to swap out the
    # existing enum with a new enum.
    # https://markrailton.com/blog/creating-migrations-when-changing-an-enum-in-python-using-sql-alchemy

    # Add HYBRID audit type
    op.execute("ALTER TYPE audittype RENAME TO audittype_old")

    new_audit_type_enum = sa.dialects.postgresql.ENUM(
        "BALLOT_POLLING",
        "BATCH_COMPARISON",
        "BALLOT_COMPARISON",
        "HYBRID",
        name="audittype",
    )
    new_audit_type_enum.create(op.get_bind())

    op.execute(
        "ALTER TABLE election ALTER COLUMN audit_type TYPE audittype USING audit_type::text::audittype"
    )

    op.execute("DROP TYPE audittype_old")

    # Add SUITE audit math type
    op.execute("ALTER TYPE auditmathtype RENAME TO auditmathtype_old")

    new_audit_math_type_enum = sa.dialects.postgresql.ENUM(
        "BRAVO", "MINERVA", "SUPERSIMPLE", "MACRO", "SUITE", name="auditmathtype"
    )
    new_audit_math_type_enum.create(op.get_bind())

    op.execute(
        "ALTER TABLE election ALTER COLUMN audit_math_type TYPE auditmathtype USING audit_math_type::text::auditmathtype"
    )

    op.execute("DROP TYPE auditmathtype_old")


def downgrade():  # pragma: no cover
    # ### commands auto generated by Alembic - please adjust! ###
    pass
    # ### end Alembic commands ###
