""" Removing foreign rule key to RuleSQL

Revision ID: 8b22879952cf
Revises: 2592f3bdae72
Create Date: 2020-02-13 20:38:38.984495

"""

# revision identifiers, used by Alembic.
revision = '8b22879952cf'
down_revision = '2592f3bdae72'
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('TRUNCATE rule_settings')
    op.add_column('rule_settings', sa.Column('file_id', sa.Integer(), nullable=True))
    op.add_column('rule_settings', sa.Column('rule_label', sa.Text(), nullable=False))
    op.add_column('rule_settings', sa.Column('target_file_id', sa.Integer(), nullable=True))
    op.drop_constraint('fk_rule', 'rule_settings', type_='foreignkey')
    op.create_foreign_key('fk_setting_target_file_type', 'rule_settings', 'file_type', ['target_file_id'], ['file_type_id'])
    op.create_foreign_key('fk_setting_file_type', 'rule_settings', 'file_type', ['file_id'], ['file_type_id'])
    op.drop_column('rule_settings', 'rule_id')
    # ### end Alembic commands ###


def downgrade_data_broker():
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute('TRUNCATE rule_settings')
    op.add_column('rule_settings', sa.Column('rule_id', sa.INTEGER(), autoincrement=False, nullable=False))
    op.drop_constraint('fk_setting_file_type', 'rule_settings', type_='foreignkey')
    op.drop_constraint('fk_setting_target_file_type', 'rule_settings', type_='foreignkey')
    op.create_foreign_key('fk_rule', 'rule_settings', 'rule_sql', ['rule_id'], ['rule_sql_id'], ondelete='CASCADE')
    op.drop_column('rule_settings', 'target_file_id')
    op.drop_column('rule_settings', 'rule_label')
    op.drop_column('rule_settings', 'file_id')
    # ### end Alembic commands ###

