"""add ondelete to foreign keys

Revision ID: 9aaefe492ae9
Revises: 65e7825b09b1
Create Date: 2019-03-13 12:31:40.909749

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '9aaefe492ae9'
down_revision = '65e7825b09b1'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('issues_project_id_fkey', 'issues', type_='foreignkey')
    op.drop_constraint('issues_user_id_fkey', 'issues', type_='foreignkey')
    op.create_foreign_key(None, 'issues', 'projects', ['project_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'issues', 'users', ['user_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint('payments_paid_user_id_fkey', 'payments', type_='foreignkey')
    op.drop_constraint('payments_payer_user_id_fkey', 'payments', type_='foreignkey')
    op.drop_constraint('payments_project_id_fkey', 'payments', type_='foreignkey')
    op.create_foreign_key(None, 'payments', 'users', ['paid_user_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'payments', 'users', ['payer_user_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'payments', 'projects', ['project_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint('project_invitations_project_id_fkey', 'project_invitations', type_='foreignkey')
    op.create_foreign_key(None, 'project_invitations', 'projects', ['project_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint('sources_user_id_fkey', 'sources', type_='foreignkey')
    op.create_foreign_key(None, 'sources', 'users', ['user_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint('time_entries_issue_id_fkey', 'time_entries', type_='foreignkey')
    op.drop_constraint('time_entries_project_id_fkey', 'time_entries', type_='foreignkey')
    op.drop_constraint('time_entries_user_id_fkey', 'time_entries', type_='foreignkey')
    op.create_foreign_key(None, 'time_entries', 'issues', ['issue_id'], ['id'], ondelete='SET NULL')
    op.create_foreign_key(None, 'time_entries', 'projects', ['project_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'time_entries', 'users', ['user_id'], ['id'], ondelete='CASCADE')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'time_entries', type_='foreignkey')
    op.drop_constraint(None, 'time_entries', type_='foreignkey')
    op.drop_constraint(None, 'time_entries', type_='foreignkey')
    op.create_foreign_key('time_entries_user_id_fkey', 'time_entries', 'users', ['user_id'], ['id'])
    op.create_foreign_key('time_entries_project_id_fkey', 'time_entries', 'projects', ['project_id'], ['id'])
    op.create_foreign_key('time_entries_issue_id_fkey', 'time_entries', 'issues', ['issue_id'], ['id'])
    op.drop_constraint(None, 'sources', type_='foreignkey')
    op.create_foreign_key('sources_user_id_fkey', 'sources', 'users', ['user_id'], ['id'])
    op.drop_constraint(None, 'project_invitations', type_='foreignkey')
    op.create_foreign_key('project_invitations_project_id_fkey', 'project_invitations', 'projects', ['project_id'], ['id'])
    op.drop_constraint(None, 'payments', type_='foreignkey')
    op.drop_constraint(None, 'payments', type_='foreignkey')
    op.drop_constraint(None, 'payments', type_='foreignkey')
    op.create_foreign_key('payments_project_id_fkey', 'payments', 'projects', ['project_id'], ['id'])
    op.create_foreign_key('payments_payer_user_id_fkey', 'payments', 'users', ['payer_user_id'], ['id'])
    op.create_foreign_key('payments_paid_user_id_fkey', 'payments', 'users', ['paid_user_id'], ['id'])
    op.drop_constraint(None, 'issues', type_='foreignkey')
    op.drop_constraint(None, 'issues', type_='foreignkey')
    op.create_foreign_key('issues_user_id_fkey', 'issues', 'users', ['user_id'], ['id'])
    op.create_foreign_key('issues_project_id_fkey', 'issues', 'projects', ['project_id'], ['id'])
    # ### end Alembic commands ###
