"""printedparts: printeditemtransactions.revision (traceability)

Records which print-file revision the physical part carried when it was taken,
captured from the label QR (gage tag + revision). Nullable - manual entry and
pre-QR labels have no revision. Idempotent guard.

Revision ID: printedparts0004txnrev
Revises: printedparts0003gagetag
Create Date: 2026-07-21

"""
from alembic import op
import sqlalchemy as sa


revision = 'printedparts0004txnrev'
down_revision = 'printedparts0003gagetag'
branch_labels = None
depends_on = None


def _has_col(insp):
    return any(c['name'] == 'revision'
               for c in insp.get_columns('printeditemtransactions'))


def upgrade():
    bind = op.get_bind()
    insp = sa.inspect(bind)
    if 'printeditemtransactions' not in insp.get_table_names():
        return
    if not _has_col(insp):
        op.add_column('printeditemtransactions',
                      sa.Column('revision', sa.Integer(), nullable=True))


def downgrade():
    bind = op.get_bind()
    insp = sa.inspect(bind)
    if 'printeditemtransactions' not in insp.get_table_names():
        return
    if _has_col(insp):
        op.drop_column('printeditemtransactions', 'revision')
