"""Remove BountyCategory table and add category FK to Bounty table

Revision ID: 189a2ac27a82
Revises: 7f70f68cde00
Create Date: 2021-03-13 23:37:50.985733

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '189a2ac27a82'
down_revision = '7f70f68cde00'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('bounty_category')
    op.add_column('bounty', sa.Column('category_id', sa.Integer(), nullable=True))
    op.drop_constraint('bounty_url_key', 'bounty', type_='unique')
    op.create_unique_constraint(op.f('uq_bounty_url'), 'bounty', ['url'])
    op.create_foreign_key(op.f('fk_bounty_category_id_category'), 'bounty', 'category', ['category_id'], ['category_id'], ondelete='NO ACTION')
    op.drop_constraint('category_name_key', 'category', type_='unique')
    op.create_unique_constraint(op.f('uq_category_name'), 'category', ['name'])
    op.drop_constraint('user_email_key', 'user', type_='unique')
    op.drop_constraint('user_username_key', 'user', type_='unique')
    op.create_unique_constraint(op.f('uq_user_email'), 'user', ['email'])
    op.create_unique_constraint(op.f('uq_user_username'), 'user', ['username'])
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(op.f('uq_user_username'), 'user', type_='unique')
    op.drop_constraint(op.f('uq_user_email'), 'user', type_='unique')
    op.create_unique_constraint('user_username_key', 'user', ['username'])
    op.create_unique_constraint('user_email_key', 'user', ['email'])
    op.drop_constraint(op.f('uq_category_name'), 'category', type_='unique')
    op.create_unique_constraint('category_name_key', 'category', ['name'])
    op.drop_constraint(op.f('fk_bounty_category_id_category'), 'bounty', type_='foreignkey')
    op.drop_constraint(op.f('uq_bounty_url'), 'bounty', type_='unique')
    op.create_unique_constraint('bounty_url_key', 'bounty', ['url'])
    op.drop_column('bounty', 'category_id')
    op.create_table('bounty_category',
    sa.Column('bounty_id', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('category_id', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['bounty_id'], ['bounty.bounty_id'], name='bounty_category_bounty_id_fkey', ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['category_id'], ['category.category_id'], name='bounty_category_category_id_fkey', ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('bounty_id', 'category_id', name='bounty_category_pkey')
    )
    # ### end Alembic commands ###
