"""Add agency_type to SQS and FileRequest tables

Revision ID: a9b778fd5181
Revises: c7da6e4c9b8a
Create Date: 2018-08-22 11:07:57.731050

"""

# revision identifiers, used by Alembic.
revision = 'a9b778fd5181'
down_revision = 'c7da6e4c9b8a'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa


def upgrade(engine_name):
    globals()["upgrade_%s" % engine_name]()


def downgrade(engine_name):
    globals()["downgrade_%s" % engine_name]()





def upgrade_data_broker():
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute("CREATE TYPE agency_types AS ENUM ('awarding', 'funding')")

    op.add_column('file_request', sa.Column('agency_type', sa.Enum('awarding', 'funding', name='agency_types'), server_default='awarding', nullable=True))
    op.execute("""
                    UPDATE file_request
                    SET agency_type = 'awarding'
                    WHERE agency_type IS NULL
                """)
    op.alter_column('file_request', 'agency_type',
                    existing_type=sa.Enum('awarding', 'funding', name='agency_types'),
                    nullable=False)
    op.create_index(op.f('ix_file_request_agency_type'), 'file_request', ['agency_type'], unique=False)

    op.add_column('sqs', sa.Column('agency_type', sa.Enum('awarding', 'funding', name='agency_types'), nullable=True))
    # ### end Alembic commands ###


def downgrade_data_broker():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('sqs', 'agency_type')
    op.drop_index(op.f('ix_file_request_agency_type'), table_name='file_request')
    op.drop_column('file_request', 'agency_type')
    op.execute("DROP TYPE agency_types")
    # ### end Alembic commands ###

