"""Data migration tas_ids to account_nums in all related tables

Revision ID: 26cfc98728c8
Revises: 46d1fec96d66
Create Date: 2017-01-30 13:23:38.526748

"""

# revision identifiers, used by Alembic.
revision = '26cfc98728c8'
down_revision = '46d1fec96d66'
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("""
        UPDATE appropriation
        SET tas_id = (SELECT account_num FROM tas_lookup WHERE tas_lookup.tas_id = appropriation.tas_id)
        WHERE appropriation.tas_id IS NOT NULL
    """)

    op.execute("""
        UPDATE object_class_program_activity
        SET tas_id = (SELECT account_num FROM tas_lookup
                      WHERE tas_lookup.tas_id = object_class_program_activity.tas_id)
        WHERE object_class_program_activity.tas_id IS NOT NULL
    """)

    op.execute("""
        UPDATE award_financial
        SET tas_id = (SELECT account_num FROM tas_lookup WHERE tas_lookup.tas_id = award_financial.tas_id)
        WHERE award_financial.tas_id IS NOT NULL
    """)

    op.execute("""
        UPDATE sf_133
        SET tas_id = (SELECT account_num FROM tas_lookup WHERE tas_lookup.tas_id = sf_133.tas_id)
        WHERE sf_133.tas_id IS NOT NULL
    """)
    ### end Alembic commands ###


def downgrade_data_broker():
    ### commands auto generated by Alembic - please adjust! ###
    op.execute("""
        UPDATE appropriation
        SET tas_id = (SELECT tas_id FROM tas_lookup WHERE tas_lookup.account_num = appropriation.tas_id)
        WHERE appropriation.tas_id IS NOT NULL
    """)

    op.execute("""
        UPDATE object_class_program_activity
        SET tas_id = (SELECT tas_id FROM tas_lookup
                      WHERE tas_lookup.account_num = object_class_program_activity.tas_id)
        WHERE object_class_program_activity.tas_id IS NOT NULL
    """)

    op.execute("""
        UPDATE award_financial
        SET tas_id = (SELECT tas_id FROM tas_lookup WHERE tas_lookup.account_num = award_financial.tas_id)
        WHERE award_financial.tas_id IS NOT NULL
    """)

    op.execute("""
        UPDATE sf_133
        SET tas_id = (SELECT tas_id FROM tas_lookup WHERE tas_lookup.account_num = sf_133.tas_id)
        WHERE sf_133.tas_id IS NOT NULL
    """)
    ### end Alembic commands ###

