"""Make ticket modifier prices nullable.

Revision ID: 3bb222243bc9
Revises: f620da5b7e8c
Create Date: 2018-02-26 17:46:50.081000

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '3bb222243bc9'
down_revision = 'f620da5b7e8c'
branch_labels = None
depends_on = None

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


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('ticket_item_modifiers') as batch_op:
            batch_op.alter_column('price', nullable=True)
    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('ticket_item_modifiers') as batch_op:
            conn.execute('UPDATE ticket_item_modifiers SET price = 0 WHERE price IS NULL')
            batch_op.alter_column('price', nullable=False)
    finally:
        conn.execute('PRAGMA foreign_keys = "1";')
