"""Payment customer name

Revision ID: 6f16d1452854
Revises: f81ad9d98879
Create Date: 2017-08-21 09:49:39.836000

"""
from alembic import op
import sqlalchemy as sa


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

first_agent_version = '17.8.22'


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

    > with op.batch_alter_table('table_name') as batch_op:
    >     pass
    """
    connection = op.get_bind()
    inspector = sa.inspect(connection)

    def _add_column_if_needed(table_name):
        cols = [col['name'] for col in inspector.get_columns(table_name)]
        if 'full_name' not in cols:
            with op.batch_alter_table(table_name) as batch_op:
                batch_op.add_column(sa.Column('full_name', sa.String(length=128), nullable=True))

    _add_column_if_needed('held_payments')
    _add_column_if_needed('payments')


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

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

    with op.batch_alter_table('payments') as batch_op:
        batch_op.drop_column('full_name')

    with op.batch_alter_table('held_payments') as batch_op:
        batch_op.drop_column('full_name')
