"""future order auto_close

Revision ID: 643fddccc803
Revises: 8882b7b20607
Create Date: 2020-01-08 07:30:51.731000

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '643fddccc803'
down_revision = '8882b7b20607'
branch_labels = None
depends_on = None

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


def upgrade():
    conn = op.get_bind()
    try:
        conn.execute('PRAGMA foreign_keys = "0";')
        with op.batch_alter_table('send_items_candidates') as batch_op:
            # 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.
            batch_op.add_column(sa.Column(
                'auto_close',
                sa.Boolean(name='send_items_candidates_auto_close_chk'),
                nullable=False,
                server_default='1',
            ))

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


def downgrade():
    conn = op.get_bind()
    try:
        conn.execute('PRAGMA foreign_keys = "0";')
        with op.batch_alter_table('send_items_candidates') as batch_op:
            batch_op.drop_column('auto_close')

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