"""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
    """

    with op.batch_alter_table('ticket_item_modifiers') as batch_op:
        batch_op.alter_column('price', nullable=True)


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

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

    with op.batch_alter_table('ticket_item_modifiers') as batch_op:
        connection = op.get_bind()

        connection.execute('UPDATE ticket_item_modifiers SET price = 0 WHERE price IS NULL')

        batch_op.alter_column('price', nullable=False)

