"""prd012: change platform_fee_amount from FLOAT to NUMERIC(14,6).

IEEE 754 double-precision floats accumulate rounding errors when summed across
thousands of rows, producing non-deterministic ACH charge amounts.  Storing
as NUMERIC(14,6) (exact fixed-point) eliminates the drift and aligns with
financial accounting requirements.

Precision 14, scale 6 allows up to $99,999,999.999999 per transaction — well
beyond any realistic single-transaction fee while preserving six decimal places
for sub-cent precision.

Revision ID: prd012_fix_platform_fee_amount_numeric
Revises: prd012_add_fee_settlement_run_id_to_transactions
Create Date: 2026-05-14
"""
from typing import Union, Sequence

import sqlalchemy as sa
from alembic import op

revision: str = "prd012_fix_platform_fee_amount_numeric"
down_revision: Union[str, Sequence[str], None] = "prd012_add_fee_settlement_run_id_to_transactions"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.alter_column(
        "transactions",
        "platform_fee_amount",
        type_=sa.Numeric(14, 6),
        existing_type=sa.Float(),
        existing_nullable=True,
        postgresql_using="platform_fee_amount::numeric(14,6)",
    )


def downgrade() -> None:
    op.alter_column(
        "transactions",
        "platform_fee_amount",
        type_=sa.Float(),
        existing_type=sa.Numeric(14, 6),
        existing_nullable=True,
        postgresql_using="platform_fee_amount::double precision",
    )
