|
1 | 1 | """Forward-only teardown of askbot.deps.group_messaging. |
2 | 2 |
|
3 | | -Postgres-targeted: relies on `DROP TABLE IF EXISTS ... CASCADE`. Also |
4 | | -removes the dep's `django_migrations` rows so Django doesn't keep |
5 | | -referencing an app that no longer exists. |
| 3 | +Drops the five model tables plus the two auto-created M2M through tables, |
| 4 | +in child-first order so plain DROP TABLE works on SQLite and MySQL. |
| 5 | +On Postgres we append CASCADE as a safety net in case a deployment has |
| 6 | +added its own FKs into these tables. |
6 | 7 |
|
7 | | -For a MySQL operator: drop the `CASCADE` keyword from each DROP (MySQL |
8 | | -behaviour differs; askbot has no FKs into these tables, so plain |
9 | | -`DROP TABLE IF EXISTS` is sufficient). For SQLite: same — drop `CASCADE`. |
| 8 | +Also removes the dep's django_migrations rows so Django stops referencing |
| 9 | +an app that no longer exists. |
10 | 10 | """ |
11 | 11 | from django.db import migrations |
12 | 12 |
|
13 | 13 |
|
| 14 | +TABLES_IN_DROP_ORDER = [ |
| 15 | + # M2M through tables first — they reference Message / SenderList. |
| 16 | + 'group_messaging_message_recipients', |
| 17 | + 'group_messaging_senderlist_senders', |
| 18 | + # Tables with FKs into Message. |
| 19 | + 'group_messaging_lastvisittime', |
| 20 | + 'group_messaging_messagememo', |
| 21 | + # SenderList is independent of Message. |
| 22 | + 'group_messaging_senderlist', |
| 23 | + # Message has a self-FK; safe to drop once the children above are gone. |
| 24 | + 'group_messaging_message', |
| 25 | + # Independent of the rest. |
| 26 | + 'group_messaging_unreadinboxcounter', |
| 27 | +] |
| 28 | + |
| 29 | + |
| 30 | +def drop_group_messaging_tables(apps, schema_editor): |
| 31 | + vendor = schema_editor.connection.vendor |
| 32 | + suffix = ' CASCADE' if vendor == 'postgresql' else '' |
| 33 | + with schema_editor.connection.cursor() as cursor: |
| 34 | + for table in TABLES_IN_DROP_ORDER: |
| 35 | + cursor.execute(f'DROP TABLE IF EXISTS {table}{suffix};') |
| 36 | + cursor.execute( |
| 37 | + "DELETE FROM django_migrations WHERE app='group_messaging';" |
| 38 | + ) |
| 39 | + |
| 40 | + |
14 | 41 | class Migration(migrations.Migration): |
15 | 42 | dependencies = [('askbot', '0036_migrate_email_validation_required')] |
16 | 43 | operations = [ |
17 | | - migrations.RunSQL( |
18 | | - sql=[ |
19 | | - "DROP TABLE IF EXISTS group_messaging_message CASCADE;", |
20 | | - "DROP TABLE IF EXISTS group_messaging_messagememo CASCADE;", |
21 | | - "DROP TABLE IF EXISTS group_messaging_senderlist CASCADE;", |
22 | | - "DROP TABLE IF EXISTS group_messaging_lastvisittime CASCADE;", |
23 | | - "DROP TABLE IF EXISTS group_messaging_unreadinboxcounter CASCADE;", |
24 | | - "DELETE FROM django_migrations WHERE app='group_messaging';", |
25 | | - ], |
26 | | - reverse_sql=migrations.RunSQL.noop, |
| 44 | + migrations.RunPython( |
| 45 | + drop_group_messaging_tables, |
| 46 | + reverse_code=migrations.RunPython.noop, |
27 | 47 | ), |
28 | 48 | ] |
0 commit comments