"""Add drivername to printerdrivers (exact INF driver name).

`location` points at the driver package; `name` is what a human calls it.
Add-PrinterDriver needs neither - it needs the driver name exactly as the INF
declares it ('HP Universal Printing PCL 6'), which nothing in the row carried.
Nullable: existing rows have no INF name until someone types it in.

Guarded/idempotent: skips when the table is absent (plugin disabled) or the
column already exists (e.g. a test DB built by db.create_all() from the model).

Revision ID: printers0003drivername
Revises: printers0002supplyalerts
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'printers0003drivername'
down_revision = 'printers0002supplyalerts'
branch_labels = None
depends_on = None


def upgrade():
    bind = op.get_bind()
    inspector = sa.inspect(bind)
    if 'printerdrivers' not in inspector.get_table_names():
        return
    columns = {column['name'] for column in inspector.get_columns('printerdrivers')}
    if 'drivername' not in columns:
        op.add_column('printerdrivers',
                      sa.Column('drivername', sa.String(length=255), nullable=True))


def downgrade():
    bind = op.get_bind()
    inspector = sa.inspect(bind)
    if 'printerdrivers' not in inspector.get_table_names():
        return
    columns = {column['name'] for column in inspector.get_columns('printerdrivers')}
    if 'drivername' in columns:
        op.drop_column('printerdrivers', 'drivername')
