"""add participant_origin column to metrics cache tables

Revision ID: 407b2b8a20d8
Revises: <KEY>
Create Date: 2019-12-17 11:51:11.516469

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '407b2b8a20d8'
down_revision = '<KEY>'
branch_labels = None
depends_on = None


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


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


def upgrade_rdr():
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute("ALTER TABLE metrics_enrollment_status_cache DROP PRIMARY KEY")
    op.execute(
        "ALTER TABLE metrics_enrollment_status_cache ADD COLUMN participant_origin VARCHAR(50) DEFAULT '';"
    )
    op.execute(
        "ALTER TABLE metrics_enrollment_status_cache ADD PRIMARY KEY (`date_inserted`,`hpo_id`,`hpo_name`,`date`,"
        "`participant_origin`)"
    )

    op.execute("ALTER TABLE metrics_gender_cache DROP PRIMARY KEY")
    op.execute(
        "ALTER TABLE metrics_gender_cache ADD COLUMN participant_origin VARCHAR(50) DEFAULT '';"
    )
    op.execute(
        "ALTER TABLE metrics_gender_cache ADD PRIMARY KEY (`date_inserted`, `type`, `enrollment_status`, `hpo_id`, "
        "`hpo_name`,`date`, `gender_name`, `participant_origin`)"
    )

    op.execute("ALTER TABLE metrics_age_cache DROP PRIMARY KEY")
    op.execute(
        "ALTER TABLE metrics_age_cache ADD COLUMN participant_origin VARCHAR(50) DEFAULT '';"
    )
    op.execute(
        "ALTER TABLE metrics_age_cache ADD PRIMARY KEY (`date_inserted`, `type`, `hpo_id`, `hpo_name`,`date`, "
        "`age_range`, `enrollment_status`, `participant_origin`)"
    )

    op.execute("ALTER TABLE metrics_race_cache DROP PRIMARY KEY")
    op.execute(
        "ALTER TABLE metrics_race_cache ADD COLUMN participant_origin VARCHAR(50) DEFAULT '';"
    )
    op.execute(
        "ALTER TABLE metrics_race_cache ADD PRIMARY KEY (`date_inserted`, `type`, `registered_flag`, "
        "`participant_flag`, `consent_flag`, `core_flag`, `hpo_id`, `hpo_name`,`date`, `participant_origin`)"
    )

    op.execute("ALTER TABLE metrics_region_cache DROP PRIMARY KEY")
    op.execute(
        "ALTER TABLE metrics_region_cache ADD COLUMN participant_origin VARCHAR(50) DEFAULT '';"
    )
    op.execute(
        "ALTER TABLE metrics_region_cache ADD PRIMARY KEY (`date_inserted`, `hpo_id`, `hpo_name`,`date`, `state_name`, "
        "`enrollment_status`, `participant_origin`)"
    )

    op.execute("ALTER TABLE metrics_lifecycle_cache DROP PRIMARY KEY")
    op.execute(
        "ALTER TABLE metrics_lifecycle_cache ADD COLUMN participant_origin VARCHAR(50) DEFAULT '';"
    )
    op.execute(
        "ALTER TABLE metrics_lifecycle_cache ADD PRIMARY KEY (`date_inserted`, `enrollment_status`, `type`, `hpo_id`,"
        "`hpo_name`,`date`, `participant_origin`)"
    )
    # ### end Alembic commands ###


def downgrade_rdr():
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute("ALTER TABLE metrics_enrollment_status_cache DROP PRIMARY KEY")
    op.drop_column('metrics_enrollment_status_cache', 'participant_origin')
    op.execute(
        "ALTER TABLE metrics_enrollment_status_cache ADD PRIMARY KEY (`date_inserted`,`hpo_id`,`hpo_name`,`date`)"
    )
    # ### end Alembic commands ###


def upgrade_metrics():
    # ### commands auto generated by Alembic - please adjust! ###
    pass
    # ### end Alembic commands ###


def downgrade_metrics():
    # ### commands auto generated by Alembic - please adjust! ###
    pass
    # ### end Alembic commands ###

