Skip to content

Commit 582c19e

Browse files
committed
fixes migration 0037_remove_group_messaging.py to work in SQLite
1 parent f3fd51d commit 582c19e

1 file changed

Lines changed: 36 additions & 16 deletions

File tree

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
11
"""Forward-only teardown of askbot.deps.group_messaging.
22
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.
67
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.
1010
"""
1111
from django.db import migrations
1212

1313

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+
1441
class Migration(migrations.Migration):
1542
dependencies = [('askbot', '0036_migrate_email_validation_required')]
1643
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,
2747
),
2848
]

0 commit comments

Comments
 (0)