|
| 1 | +"""Add ondelete cascade to foreign keys |
| 2 | +
|
| 3 | +Revision ID: b295b033364d |
| 4 | +Revises: b5551cd26764 |
| 5 | +Create Date: 2019-05-03 19:26:57.746887 |
| 6 | +
|
| 7 | +""" |
| 8 | +from alembic import op |
| 9 | +import sqlalchemy as sa |
| 10 | +from sqlalchemy.dialects import mysql |
| 11 | + |
| 12 | +# revision identifiers, used by Alembic. |
| 13 | +revision = 'b295b033364d' |
| 14 | +down_revision = 'b5551cd26764' |
| 15 | +branch_labels = None |
| 16 | +depends_on = None |
| 17 | + |
| 18 | + |
| 19 | +def upgrade(): |
| 20 | + bind = op.get_bind() |
| 21 | + url = str(bind.engine.url) |
| 22 | + if url.startswith('mysql'): |
| 23 | + op.drop_constraint('awards_ibfk_1', 'awards', type_='foreignkey') |
| 24 | + op.drop_constraint('awards_ibfk_2', 'awards', type_='foreignkey') |
| 25 | + op.create_foreign_key('awards_ibfk_1', 'awards', 'teams', ['team_id'], ['id'], ondelete='CASCADE') |
| 26 | + op.create_foreign_key('awards_ibfk_2', 'awards', 'users', ['user_id'], ['id'], ondelete='CASCADE') |
| 27 | + |
| 28 | + op.drop_constraint('files_ibfk_1', 'files', type_='foreignkey') |
| 29 | + op.create_foreign_key('files_ibfk_1', 'files', 'challenges', ['challenge_id'], ['id'], ondelete='CASCADE') |
| 30 | + |
| 31 | + op.drop_constraint('flags_ibfk_1', 'flags', type_='foreignkey') |
| 32 | + op.create_foreign_key('flags_ibfk_1', 'flags', 'challenges', ['challenge_id'], ['id'], ondelete='CASCADE') |
| 33 | + |
| 34 | + op.drop_constraint('hints_ibfk_1', 'hints', type_='foreignkey') |
| 35 | + op.create_foreign_key('hints_ibfk_1', 'hints', 'challenges', ['challenge_id'], ['id'], ondelete='CASCADE') |
| 36 | + |
| 37 | + op.drop_constraint('tags_ibfk_1', 'tags', type_='foreignkey') |
| 38 | + op.create_foreign_key('tags_ibfk_1', 'tags', 'challenges', ['challenge_id'], ['id'], ondelete='CASCADE') |
| 39 | + |
| 40 | + op.drop_constraint('team_captain_id', 'teams', type_='foreignkey') |
| 41 | + op.create_foreign_key('team_captain_id', 'teams', 'users', ['captain_id'], ['id'], ondelete='SET NULL') |
| 42 | + |
| 43 | + op.drop_constraint('tracking_ibfk_1', 'tracking', type_='foreignkey') |
| 44 | + op.create_foreign_key('tracking_ibfk_1', 'tracking', 'users', ['user_id'], ['id'], ondelete='CASCADE') |
| 45 | + |
| 46 | + op.drop_constraint('unlocks_ibfk_1', 'unlocks', type_='foreignkey') |
| 47 | + op.drop_constraint('unlocks_ibfk_2', 'unlocks', type_='foreignkey') |
| 48 | + op.create_foreign_key('unlocks_ibfk_1', 'unlocks', 'teams', ['team_id'], ['id'], ondelete='CASCADE') |
| 49 | + op.create_foreign_key('unlocks_ibfk_2', 'unlocks', 'users', ['user_id'], ['id'], ondelete='CASCADE') |
| 50 | + elif url.startswith('postgres'): |
| 51 | + op.drop_constraint('awards_team_id_fkey', 'awards', type_='foreignkey') |
| 52 | + op.drop_constraint('awards_user_id_fkey', 'awards', type_='foreignkey') |
| 53 | + op.create_foreign_key('awards_team_id_fkey', 'awards', 'teams', ['team_id'], ['id'], ondelete='CASCADE') |
| 54 | + op.create_foreign_key('awards_user_id_fkey', 'awards', 'users', ['user_id'], ['id'], ondelete='CASCADE') |
| 55 | + |
| 56 | + op.drop_constraint('files_challenge_id_fkey', 'files', type_='foreignkey') |
| 57 | + op.create_foreign_key('files_challenge_id_fkey', 'files', 'challenges', ['challenge_id'], ['id'], ondelete='CASCADE') |
| 58 | + |
| 59 | + op.drop_constraint('flags_challenge_id_fkey', 'flags', type_='foreignkey') |
| 60 | + op.create_foreign_key('flags_challenge_id_fkey', 'flags', 'challenges', ['challenge_id'], ['id'], ondelete='CASCADE') |
| 61 | + |
| 62 | + op.drop_constraint('hints_challenge_id_fkey', 'hints', type_='foreignkey') |
| 63 | + op.create_foreign_key('hints_challenge_id_fkey', 'hints', 'challenges', ['challenge_id'], ['id'], ondelete='CASCADE') |
| 64 | + |
| 65 | + op.drop_constraint('tags_challenge_id_fkey', 'tags', type_='foreignkey') |
| 66 | + op.create_foreign_key('tags_challenge_id_fkey', 'tags', 'challenges', ['challenge_id'], ['id'], ondelete='CASCADE') |
| 67 | + |
| 68 | + op.drop_constraint('team_captain_id', 'teams', type_='foreignkey') |
| 69 | + op.create_foreign_key('team_captain_id', 'teams', 'users', ['captain_id'], ['id'], ondelete='SET NULL') |
| 70 | + |
| 71 | + op.drop_constraint('tracking_user_id_fkey', 'tracking', type_='foreignkey') |
| 72 | + op.create_foreign_key('tracking_user_id_fkey', 'tracking', 'users', ['user_id'], ['id'], ondelete='CASCADE') |
| 73 | + |
| 74 | + op.drop_constraint('unlocks_team_id_fkey', 'unlocks', type_='foreignkey') |
| 75 | + op.drop_constraint('unlocks_user_id_fkey', 'unlocks', type_='foreignkey') |
| 76 | + op.create_foreign_key('unlocks_team_id_fkey', 'unlocks', 'teams', ['team_id'], ['id'], ondelete='CASCADE') |
| 77 | + op.create_foreign_key('unlocks_user_id_fkey', 'unlocks', 'users', ['user_id'], ['id'], ondelete='CASCADE') |
| 78 | + |
| 79 | + |
| 80 | +def downgrade(): |
| 81 | + bind = op.get_bind() |
| 82 | + url = str(bind.engine.url) |
| 83 | + if url.startswith('mysql'): |
| 84 | + op.drop_constraint('unlocks_ibfk_1', 'unlocks', type_='foreignkey') |
| 85 | + op.drop_constraint('unlocks_ibfk_2', 'unlocks', type_='foreignkey') |
| 86 | + op.create_foreign_key('unlocks_ibfk_1', 'unlocks', 'teams', ['team_id'], ['id']) |
| 87 | + op.create_foreign_key('unlocks_ibfk_2', 'unlocks', 'users', ['user_id'], ['id']) |
| 88 | + |
| 89 | + op.drop_constraint('tracking_ibfk_1', 'tracking', type_='foreignkey') |
| 90 | + op.create_foreign_key('tracking_ibfk_1', 'tracking', 'users', ['user_id'], ['id']) |
| 91 | + |
| 92 | + op.drop_constraint('team_captain_id', 'teams', type_='foreignkey') |
| 93 | + op.create_foreign_key('team_captain_id', 'teams', 'users', ['captain_id'], ['id']) |
| 94 | + |
| 95 | + op.drop_constraint('tags_ibfk_1', 'tags', type_='foreignkey') |
| 96 | + op.create_foreign_key('tags_ibfk_1', 'tags', 'challenges', ['challenge_id'], ['id']) |
| 97 | + |
| 98 | + op.drop_constraint('hints_ibfk_1', 'hints', type_='foreignkey') |
| 99 | + op.create_foreign_key('hints_ibfk_1', 'hints', 'challenges', ['challenge_id'], ['id']) |
| 100 | + |
| 101 | + op.drop_constraint('flags_ibfk_1', 'flags', type_='foreignkey') |
| 102 | + op.create_foreign_key('flags_ibfk_1', 'flags', 'challenges', ['challenge_id'], ['id']) |
| 103 | + |
| 104 | + op.drop_constraint('files_ibfk_1', 'files', type_='foreignkey') |
| 105 | + op.create_foreign_key('files_ibfk_1', 'files', 'challenges', ['challenge_id'], ['id']) |
| 106 | + |
| 107 | + op.drop_constraint('awards_ibfk_1', 'awards', type_='foreignkey') |
| 108 | + op.drop_constraint('awards_ibfk_2', 'awards', type_='foreignkey') |
| 109 | + op.create_foreign_key('awards_ibfk_1', 'awards', 'teams', ['team_id'], ['id']) |
| 110 | + op.create_foreign_key('awards_ibfk_2', 'awards', 'users', ['user_id'], ['id']) |
| 111 | + elif url.startswith('postgres'): |
| 112 | + op.drop_constraint('unlocks_team_id_fkey', 'unlocks', type_='foreignkey') |
| 113 | + op.drop_constraint('unlocks_user_id_fkey', 'unlocks', type_='foreignkey') |
| 114 | + op.create_foreign_key('unlocks_team_id_fkey', 'unlocks', 'teams', ['team_id'], ['id']) |
| 115 | + op.create_foreign_key('unlocks_user_id_fkey', 'unlocks', 'users', ['user_id'], ['id']) |
| 116 | + |
| 117 | + op.drop_constraint('tracking_user_id_fkey', 'tracking', type_='foreignkey') |
| 118 | + op.create_foreign_key('tracking_user_id_fkey', 'tracking', 'users', ['user_id'], ['id']) |
| 119 | + |
| 120 | + op.drop_constraint('team_captain_id', 'teams', type_='foreignkey') |
| 121 | + op.create_foreign_key('team_captain_id', 'teams', 'users', ['captain_id'], ['id']) |
| 122 | + |
| 123 | + op.drop_constraint('tags_challenge_id_fkey', 'tags', type_='foreignkey') |
| 124 | + op.create_foreign_key('tags_challenge_id_fkey', 'tags', 'challenges', ['challenge_id'], ['id']) |
| 125 | + |
| 126 | + op.drop_constraint('hints_challenge_id_fkey', 'hints', type_='foreignkey') |
| 127 | + op.create_foreign_key('hints_challenge_id_fkey', 'hints', 'challenges', ['challenge_id'], ['id']) |
| 128 | + |
| 129 | + op.drop_constraint('flags_challenge_id_fkey', 'flags', type_='foreignkey') |
| 130 | + op.create_foreign_key('flags_challenge_id_fkey', 'flags', 'challenges', ['challenge_id'], ['id']) |
| 131 | + |
| 132 | + op.drop_constraint('files_challenge_id_fkey', 'files', type_='foreignkey') |
| 133 | + op.create_foreign_key('files_challenge_id_fkey', 'files', 'challenges', ['challenge_id'], ['id']) |
| 134 | + |
| 135 | + op.drop_constraint('awards_team_id_fkey', 'awards', type_='foreignkey') |
| 136 | + op.drop_constraint('awards_user_id_fkey', 'awards', type_='foreignkey') |
| 137 | + op.create_foreign_key('awards_team_id_fkey', 'awards', 'teams', ['team_id'], ['id']) |
| 138 | + op.create_foreign_key('awards_user_id_fkey', 'awards', 'users', ['user_id'], ['id']) |
| 139 | + |
| 140 | + |
0 commit comments