"""fix duplicate enum values

Revision ID: 38741c85f228
Revises: 31463788c713
Create Date: 2024-05-27 15:33:27.394135

"""

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

# revision identifiers, used by Alembic.
revision = "38741c85f228"
down_revision = "31463788c713"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    conn = op.get_bind()
    conn.exec_driver_sql("ALTER TYPE routestatus ADD VALUE 'terminating'")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    conn = op.get_bind()
    conn.exec_driver_sql(
        "UPDATE routings SET status = 'unhealthy'::routestatus WHERE status = 'terminating'::routestatus"
    )
    op.alter_column("routings", "status", type_=sa.TEXT)

    routestatus_choices = ["healthy", "unhealthy", "failed_to_start", "provisioning"]
    routestatus = postgresql.ENUM(*routestatus_choices, name="routestatus")
    routestatus.drop(conn)
    routestatus.create(conn)

    op.alter_column("routings", "status", type_=routestatus, postgresql_using="status::routestatus")

    # ### end Alembic commands ###
