<gh_stars>1-10
"""link questions to ministers.

Revision ID: 2e0ca9f0afc6
Revises: <KEY>
Create Date: 2015-09-03 12:46:06.589115

"""

# revision identifiers, used by Alembic.
revision = '2e0ca9f0afc6'
down_revision = '<KEY>'

from alembic import op
import sqlalchemy as sa


def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('committee_question', sa.Column('minister_id', sa.Integer(), nullable=True))
    op.create_foreign_key('committee_question_minister_id_fkey', 'committee_question', 'minister', ['minister_id'], ['id'])
    op.add_column('question_reply', sa.Column('minister_id', sa.Integer(), nullable=True))
    op.create_foreign_key('question_reply_minister_id_fkey', 'question_reply', 'minister', ['minister_id'], ['id'])
    ### end Alembic commands ###

    op.execute("""
    UPDATE committee_question AS qc
    SET minister_id = c.minister_id
    FROM committee c
    WHERE qc.committee_id = c.id
    """)

    op.execute("""
    UPDATE question_reply AS qr
    SET minister_id = c.minister_id
    FROM committee c
    WHERE qr.committee_id = c.id
    """)


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('question_reply_minister_id_fkey', 'question_reply', type_='foreignkey')
    op.drop_column('question_reply', 'minister_id')
    op.drop_constraint('committee_question_minister_id_fkey', 'committee_question', type_='foreignkey')
    op.drop_column('committee_question', 'minister_id')
    ### end Alembic commands ###
