"""balance_logs: unique constraint on (ref_no, change_type) for recharge dedup

Revision ID: 017_balance_logs_unique_ref
Revises: 016_recharge_and_balance
Create Date: 2026-05-18
"""

from alembic import op

revision = "017_balance_logs_unique_ref"
down_revision = "016_recharge_and_balance"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # 充值流水按 (ref_no, change_type) 唯一，防止重放攻击写入重复流水
    op.create_index(
        "uq_balance_logs_ref_no_change_type",
        "balance_logs",
        ["ref_no", "change_type"],
        unique=True,
        postgresql_where="ref_no IS NOT NULL",
    )


def downgrade() -> None:
    op.drop_index("uq_balance_logs_ref_no_change_type", table_name="balance_logs")
