"""printers: printersupplyalerts table (toner alert crossing state).

Stores the last tier alerted per (printer, supply) so the poller fires once
on a downward crossing and re-arms after a refill. Idempotent create.
"""
from alembic import op
import sqlalchemy as sa


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


def upgrade():
    bind = op.get_bind()
    inspector = sa.inspect(bind)
    if 'printersupplyalerts' in inspector.get_table_names():
        return
    op.create_table(
        'printersupplyalerts',
        sa.Column('printersupplyalertid', sa.Integer(), primary_key=True),
        sa.Column('printerid', sa.Integer(), nullable=False),
        sa.Column('supplykey', sa.String(length=64), nullable=False),
        sa.Column('lasttier', sa.String(length=16), nullable=False,
                  server_default='ok'),
        sa.Column('createddate', sa.DateTime(), nullable=False),
        sa.Column('modifieddate', sa.DateTime(), nullable=False),
        sa.Column('isactive', sa.Boolean(), nullable=False,
                  server_default=sa.true()),
        sa.ForeignKeyConstraint(['printerid'], ['printers.printerid'],
                                ondelete='CASCADE'),
        sa.UniqueConstraint('printerid', 'supplykey',
                            name='uq_printersupplyalert_printer_key'),
    )
    op.create_index('ix_printersupplyalerts_printerid',
                    'printersupplyalerts', ['printerid'])


def downgrade():
    bind = op.get_bind()
    inspector = sa.inspect(bind)
    if 'printersupplyalerts' not in inspector.get_table_names():
        return
    op.drop_table('printersupplyalerts')
