"""add ticket ready date and time

Revision ID: e0cd4ca3926c
Revises: d3f8a6200931
Create Date: 2018-12-13 18:58:05.148000

"""
from alembic import op
from sqlalchemy import Column, Date, Time


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

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


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('ready_date', Date, nullable=True))
            batch_op.add_column(Column('ready_time', Time, nullable=True))

        with op.batch_alter_table('held_tickets') as batch_op:
            batch_op.add_column(Column('ready_date', Date, nullable=True))
            batch_op.add_column(Column('ready_time', Time, 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('tickets') as batch_op:
            batch_op.drop_column('ready_date')
            batch_op.drop_column('ready_time')

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

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