|
1
|
|
|
"""add cascade delete to foreign key constraints |
|
2
|
|
|
|
|
3
|
|
|
Revision ID: add_cascade_delete |
|
4
|
|
|
Revises: 67d8fbce85bb |
|
5
|
|
|
Create Date: 2025-12-27 21:30:00.000000 |
|
6
|
|
|
|
|
7
|
|
|
""" |
|
8
|
|
|
from alembic import op |
|
9
|
|
|
import sqlalchemy as sa |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
# revision identifiers, used by Alembic. |
|
13
|
|
|
revision = 'add_cascade_delete' |
|
14
|
|
|
down_revision = '67d8fbce85bb' |
|
15
|
|
|
branch_labels = None |
|
16
|
|
|
depends_on = None |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
def upgrade(): |
|
20
|
|
|
# SQLite doesn't support ALTER TABLE for foreign keys, so we need to recreate tables |
|
21
|
|
|
# We'll use batch operations which handle the table recreation for us |
|
22
|
|
|
|
|
23
|
|
|
with op.batch_alter_table('user', schema=None) as batch_op: |
|
24
|
|
|
batch_op.drop_constraint(None, type_='foreignkey') |
|
25
|
|
|
batch_op.create_foreign_key('fk_user_account_owner', 'user', ['account_owner_id'], ['id'], ondelete='CASCADE') |
|
26
|
|
|
|
|
27
|
|
|
with op.batch_alter_table('balance', schema=None) as batch_op: |
|
28
|
|
|
batch_op.drop_constraint(None, type_='foreignkey') |
|
29
|
|
|
batch_op.create_foreign_key('fk_balance_user', 'user', ['user_id'], ['id'], ondelete='CASCADE') |
|
30
|
|
|
|
|
31
|
|
|
with op.batch_alter_table('email', schema=None) as batch_op: |
|
32
|
|
|
batch_op.drop_constraint(None, type_='foreignkey') |
|
33
|
|
|
batch_op.create_foreign_key('fk_email_user', 'user', ['user_id'], ['id'], ondelete='CASCADE') |
|
34
|
|
|
|
|
35
|
|
|
with op.batch_alter_table('hold', schema=None) as batch_op: |
|
36
|
|
|
batch_op.drop_constraint(None, type_='foreignkey') |
|
37
|
|
|
batch_op.create_foreign_key('fk_hold_user', 'user', ['user_id'], ['id'], ondelete='CASCADE') |
|
38
|
|
|
|
|
39
|
|
|
with op.batch_alter_table('schedule', schema=None) as batch_op: |
|
40
|
|
|
batch_op.drop_constraint(None, type_='foreignkey') |
|
41
|
|
|
batch_op.create_foreign_key('fk_schedule_user', 'user', ['user_id'], ['id'], ondelete='CASCADE') |
|
42
|
|
|
|
|
43
|
|
|
with op.batch_alter_table('skip', schema=None) as batch_op: |
|
44
|
|
|
batch_op.drop_constraint(None, type_='foreignkey') |
|
45
|
|
|
batch_op.create_foreign_key('fk_skip_user', 'user', ['user_id'], ['id'], ondelete='CASCADE') |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
def downgrade(): |
|
49
|
|
|
# Revert to foreign keys without cascade delete |
|
50
|
|
|
|
|
51
|
|
|
with op.batch_alter_table('skip', schema=None) as batch_op: |
|
52
|
|
|
batch_op.drop_constraint('fk_skip_user', type_='foreignkey') |
|
53
|
|
|
batch_op.create_foreign_key(None, 'user', ['user_id'], ['id']) |
|
54
|
|
|
|
|
55
|
|
|
with op.batch_alter_table('schedule', schema=None) as batch_op: |
|
56
|
|
|
batch_op.drop_constraint('fk_schedule_user', type_='foreignkey') |
|
57
|
|
|
batch_op.create_foreign_key(None, 'user', ['user_id'], ['id']) |
|
58
|
|
|
|
|
59
|
|
|
with op.batch_alter_table('hold', schema=None) as batch_op: |
|
60
|
|
|
batch_op.drop_constraint('fk_hold_user', type_='foreignkey') |
|
61
|
|
|
batch_op.create_foreign_key(None, 'user', ['user_id'], ['id']) |
|
62
|
|
|
|
|
63
|
|
|
with op.batch_alter_table('email', schema=None) as batch_op: |
|
64
|
|
|
batch_op.drop_constraint('fk_email_user', type_='foreignkey') |
|
65
|
|
|
batch_op.create_foreign_key(None, 'user', ['user_id'], ['id']) |
|
66
|
|
|
|
|
67
|
|
|
with op.batch_alter_table('balance', schema=None) as batch_op: |
|
68
|
|
|
batch_op.drop_constraint('fk_balance_user', type_='foreignkey') |
|
69
|
|
|
batch_op.create_foreign_key(None, 'user', ['user_id'], ['id']) |
|
70
|
|
|
|
|
71
|
|
|
with op.batch_alter_table('user', schema=None) as batch_op: |
|
72
|
|
|
batch_op.drop_constraint('fk_user_account_owner', type_='foreignkey') |
|
73
|
|
|
batch_op.create_foreign_key(None, 'user', ['account_owner_id'], ['id']) |
|
74
|
|
|
|