"""ticket terminal

Revision ID: d3f8a6200931
Revises: 30b31a9825cf
Create Date: 2018-12-29 08:21:20.187000

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import Column, String, Integer, inspect


# revision identifiers, used by Alembic.
revision = 'd3f8a6200931'
down_revision = 'e27e807c1ad0'
branch_labels = None
depends_on = None

first_agent_version = '19.1.1'


def upgrade():
    """
    Use batch-operations to support sqlite

    > with op.batch_alter_table('table_name') as batch_op:
    >     pass
    """
    connection = op.get_bind()
    try:
        connection.execute('PRAGMA foreign_keys = "0";')

        with op.batch_alter_table('held_tickets') as batch_op:
            batch_op.add_column(Column('terminal_id', String(length=64), nullable=True))

        with op.batch_alter_table('tickets') as batch_op:
            batch_op.add_column(Column('terminal_id', String(length=64), nullable=True))

    finally:
        connection.execute('PRAGMA foreign_keys = "1";')


def downgrade():
    """
    Use batch-operations to support sqlite

    > with op.batch_alter_table('table_name') as batch_op:
    >     pass
    """
    connection = op.get_bind()
    try:
        connection.execute('PRAGMA foreign_keys = "0";')

        with op.batch_alter_table('held_tickets') as batch_op:
            batch_op.drop_column('terminal_id')

        with op.batch_alter_table('tickets') as batch_op:
            batch_op.drop_column('terminal_id')

    finally:
        connection.execute('PRAGMA foreign_keys = "1";')
