"""MPS-20: auction quantity column

Revision ID: 1a419114ddf7
Revises: 34e1c65d0bea
Create Date: 2026-05-10 10:32:31.725890

Adds MpsAuction.quantity (Integer, default 1) so a single auction can
sell more than one unit at once. For physical products, owner can lot
N units of inventory; the other inventory stays at list price.

Idempotent — make init-db creates tables from models.
"""
from alembic import op
import sqlalchemy as sa


revision = '1a419114ddf7'
down_revision = '34e1c65d0bea'
branch_labels = None
depends_on = None


def _column_exists(table, column):
    conn = op.get_bind()
    result = conn.execute(sa.text(f"PRAGMA table_info({table})"))
    return any(row[1] == column for row in result.fetchall())


def upgrade():
    if not _column_exists("mps_auction", "quantity"):
        op.add_column(
            "mps_auction",
            sa.Column("quantity", sa.Integer(), server_default="1", nullable=False),
        )


def downgrade():
    if _column_exists("mps_auction", "quantity"):
        op.drop_column("mps_auction", "quantity")
