"""Record the display subtype a PC reports for itself.

A kiosk knows whether it is a Dashboard, Lobby or 3DPrintRoom - the dispatcher
reads C:\\Enrollment\\display-type.txt to pick its target - but it never told
ShopDB, so the reports table had to infer it from the DashboardDefault
fqdn/ip mapping, which is blank unless someone added a row per kiosk. Reported
by the device beats inferred from a mapping table, the same way enforcerversion
already works.

Revision ID: geenforce0003subtype
Revises: geenforce0002blobs
"""
from alembic import op
import sqlalchemy as sa

revision = 'geenforce0003subtype'
down_revision = 'geenforce0002blobs'
branch_labels = None
depends_on = None


def _columns(bind):
    insp = sa.inspect(bind)
    if 'manifestenforcementreports' not in insp.get_table_names():
        return None
    return {c['name'] for c in insp.get_columns('manifestenforcementreports')}


def upgrade():
    # Guarded, like network0003prefix. On a FRESH database the table is built
    # from the SQLAlchemy models, which already declare this column, so an
    # unconditional add fails with "duplicate column name" - and this chain has
    # been re-running its head on MySQL (see the missing commit in
    # shopdb/plugins/alembic_template.py), so a redeploy hit the same wall.
    columns = _columns(op.get_bind())
    if columns is None or 'subtype' in columns:
        return
    op.add_column('manifestenforcementreports',
                  sa.Column('subtype', sa.String(length=50), nullable=True))


def downgrade():
    columns = _columns(op.get_bind())
    if columns is None or 'subtype' not in columns:
        return
    op.drop_column('manifestenforcementreports', 'subtype')
