"""remove_table_available

Revision ID: 318e913edf0a
Revises: 62f8d9027964
Create Date: 2018-10-17 17:55:10.186000

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '318e913edf0a'
down_revision = '62f8d9027964'
branch_labels = None
depends_on = None

first_agent_version = '18.10.18'


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('tables') as batch_op:
            batch_op.drop_column('available')

    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";')

        # the name kwarg is for the check constraint, required by SA for DB engines that store bool as `0` or `1` to ensure
        # it. Although SQL Lite doesn't seem to support check constraints, this prevents a SqlAlchemy exception.
        with op.batch_alter_table('tables') as batch_op:
            batch_op.add_column(sa.Column('available', sa.Boolean(name='tables_available_chk')))

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