"""add ticket pos_id

Revision ID: 1329d9147b45
Revises: 4d0e57a6de81
Create Date: 2019-08-12 12:19:31.076000

"""
from alembic import op
from sqlalchemy import (
    Column,
    String,
)


# revision identifiers, used by Alembic.
revision = '1329d9147b45'
down_revision = '4d0e57a6de81'
branch_labels = None
depends_on = None

# TODO: Set the first_agent_version correctly before merging this revision
first_agent_version = '19.8.12'


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('tickets') as batch_op:
            batch_op.add_column(Column('pos_id', String(length=128), nullable=True))

        with op.batch_alter_table('held_tickets') as batch_op:
            batch_op.add_column(Column('pos_id', String(length=128), 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('pos_id')

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

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