"""cast_as_date function index on fpds and fabs

Revision ID: b168f0cdc5a8
Revises: 5dbc09749ce0
Create Date: 2018-01-18 16:33:47.928716

"""

# revision identifiers, used by Alembic.
revision = 'b168f0cdc5a8'
down_revision = '5dbc09749ce0'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

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 OR REPLACE FUNCTION cast_as_date(TEXT) RETURNS DATE
        AS 'select cast($1 as DATE)'
        LANGUAGE SQL
        IMMUTABLE
        RETURNS NULL ON NULL INPUT
    """)
    op.execute("""CREATE INDEX ix_fpds_cast_action_date_as_date ON detached_award_procurement (cast_as_date(action_date))""")
    op.execute("""CREATE INDEX ix_fabs_cast_action_date_as_date ON published_award_financial_assistance (cast_as_date(action_date))""")
    ### end Alembic commands ###


def downgrade_data_broker():
    ### commands auto generated by Alembic - please adjust! ###
    op.execute("""DROP INDEX IF EXISTS ix_fpds_cast_action_date_as_date""")
    op.execute("""DROP INDEX IF EXISTS ix_fabs_cast_action_date_as_date""")
    op.execute("""DROP FUNCTION IF EXISTS cast_as_date(TEXT)""")
    ### end Alembic commands ###

