"""add_rate_to_clock_entry_segment

Revision ID: d6ce18d761a3
Revises: b451ab2b9136
Create Date: 2020-01-15 18:28:22.103000

"""
from alembic import op
from sqlalchemy import Column, Integer


# revision identifiers, used by Alembic.
revision = 'd6ce18d761a3'
down_revision = 'b451ab2b9136'
branch_labels = None
depends_on = None

first_agent_version = '20.1.27'


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('clock_entry_segments') as batch_op:
            batch_op.add_column(Column('rate', Integer, nullable=True))

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

        with op.batch_alter_table('clock_entry_segments') as batch_op:
            batch_op.drop_column('rate')

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