"""add withdraw concurrency safety indexes

Revision ID: withdraw_safety_indexes
Revises: 0cf18d590b1d
Create Date: 2026-06-08 16:20:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    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')"),
    )
    op.create_index(
        "ux_cash_transaction_withdraw_refund_ref",
        "cash_transaction",
        ["ref_id"],
        unique=True,
        sqlite_where=sa.text("biz_type = 'withdraw_refund' AND ref_id IS NOT NULL"),
        postgresql_where=sa.text("biz_type = 'withdraw_refund' AND ref_id IS NOT NULL"),
    )


def downgrade() -> None:
    op.drop_index("ux_cash_transaction_withdraw_refund_ref", table_name="cash_transaction")
    op.drop_index("ux_withdraw_order_user_active", table_name="withdraw_order")
