"""add_user_fk_to_submission

Revision ID: 885280875a1c
Revises: c9e8302571cd
Create Date: 2016-10-23 15:41:42.023560

"""

# revision identifiers, used by Alembic.
revision = '885280875a1c'
down_revision = 'c9e8302571cd'
branch_labels = None
depends_on = None

from alembic import op


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


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





def upgrade_data_broker():
    # unset existing users' ids
    op.execute("""
        UPDATE submission
        SET user_id = NULL
        WHERE user_id NOT IN (SELECT user_id FROM users)
    """)
    ### commands auto generated by Alembic - please adjust! ###
    op.create_foreign_key('fk_submission_user', 'submission', 'users', ['user_id'], ['user_id'], ondelete='SET NULL')
    ### end Alembic commands ###


def downgrade_data_broker():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('fk_submission_user', 'submission', type_='foreignkey')
    ### end Alembic commands ###

