"""Support apply service charges to held ticket

Revision ID: 8fed416518e1
Revises: e88d67c3ccf9
Create Date: 2017-02-14 12:37:52.087000

"""
import json

from alembic import op
from sqlalchemy import Column, PickleType


# revision identifiers, used by Alembic.
revision = '8fed416518e1'
down_revision = 'e88d67c3ccf9'
branch_labels = None
depends_on = None

first_agent_version = '17.2.20'


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

    > with op.batch_alter_table('table_name') as batch_op:
    >     pass
    """

    conn = op.get_bind()
    try:
        conn.execute('PRAGMA foreign_keys = "0";')
        with op.batch_alter_table('held_tickets') as batch_op:
            batch_op.add_column(Column('held_service_charge_ids', PickleType(pickler=json), nullable=False, server_default='[]'))
    finally:
        conn.execute('PRAGMA foreign_keys = "1";')


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

    > with op.batch_alter_table('table_name') as batch_op:
    >     pass
    """
    conn = op.get_bind()
    try:
        conn.execute('PRAGMA foreign_keys = "0";')
        with op.batch_alter_table('held_tickets') as batch_op:
            batch_op.drop_column('held_service_charge_ids')
    finally:
        conn.execute('PRAGMA foreign_keys = "1";')

