"""drop withdraw active-order partial unique index (allow multiple in-flight withdrawals)

Revision ID: drop_withdraw_active_uniq_idx
Revises: d8dd2106e438
Create Date: 2026-07-24 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "drop_withdraw_active_uniq_idx"
down_revision: Union[str, Sequence[str], None] = "d8dd2106e438"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # 取消「同一用户同一时刻仅一笔在途提现」:允许 reviewing/pending 并存。
    # 仅删本索引;姊妹索引 ux_cash_transaction_withdraw_refund_ref(退款幂等)保持不动。
    op.drop_index("ux_withdraw_order_user_active", table_name="withdraw_order")


def downgrade() -> None:
    # 回滚重建分区唯一索引。注意:若届时某用户已有 ≥2 张在途单,重建会因唯一冲突失败——
    # 属预期的回滚代价(取消限制后本就允许多单),需先人工收敛在途单再回滚。
    op.create_index(
        "ux_withdraw_order_user_active",
        "withdraw_order",
        ["user_id"],
        unique=True,
        sqlite_where=sa.text("status IN ('reviewing', 'pending')"),
        postgresql_where=sa.text("status IN ('reviewing', 'pending')"),
    )
