"""Make ClockEntrySegment.pay nullable

Revision ID: 4b331743953d
Revises: 6f16d1452854
Create Date: 2017-09-26 22:02:56.679780

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '4b331743953d'
down_revision = '6f16d1452854'
branch_labels = None
depends_on = None

first_agent_version = '17.9.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('clock_entry_segments') as batch_op:
        batch_op.alter_column('pay', nullable=True)


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

    > with op.batch_alter_table('table_name') as batch_op:
    >     pass
    """
    connection = op.get_bind()
    connection.execute('UPDATE clock_entry_segments SET pay = 0 WHERE pay is NULL')

    with op.batch_alter_table('clock_entry_segments') as batch_op:
        batch_op.alter_column('pay', nullable=False)
