Skip to content

Commit 8531281

Browse files
committed
Add migration to drop speakers app database tables
Creates core migration 0010_remove_speakers_tables.py that: - Drops all database tables from the removed speakers app - Handles table dependencies correctly (CASCADE) - Drops tables in order: junction tables, then FK tables, then base tables - Is irreversible (requires backup to rollback) Tables dropped: - speakers_session_speakers (M2M junction table) - speakers_session - speakers_speaker - speakers_room - speakers_talkspage - speakers_speakerspage This migration should be run on production before deploying the code that removes the speakers app from INSTALLED_APPS.
1 parent 37470c5 commit 8531281

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Generated manually to remove speakers app tables
2+
3+
from django.db import migrations
4+
5+
6+
def drop_speakers_tables(apps, schema_editor):
7+
"""
8+
Drop all tables from the removed speakers app.
9+
10+
This migration removes tables that were part of the speakers app which
11+
has been completely removed from the codebase. The tables are dropped
12+
in order to handle foreign key constraints properly.
13+
"""
14+
# We can't use apps.get_model() here since the speakers app is removed
15+
# Instead, we use raw SQL to drop the tables
16+
with schema_editor.connection.cursor() as cursor:
17+
# Drop tables in reverse dependency order
18+
# First drop junction tables (M2M relationships)
19+
cursor.execute("DROP TABLE IF EXISTS speakers_session_speakers CASCADE")
20+
21+
# Then drop tables with foreign keys
22+
cursor.execute("DROP TABLE IF EXISTS speakers_session CASCADE")
23+
cursor.execute("DROP TABLE IF EXISTS speakers_speaker CASCADE")
24+
25+
# Drop remaining tables
26+
cursor.execute("DROP TABLE IF EXISTS speakers_room CASCADE")
27+
cursor.execute("DROP TABLE IF EXISTS speakers_talkspage CASCADE")
28+
cursor.execute("DROP TABLE IF EXISTS speakers_speakerspage CASCADE")
29+
30+
31+
def reverse_drop_speakers_tables(apps, schema_editor):
32+
"""
33+
This migration is irreversible.
34+
35+
If you need to rollback, restore from a database backup taken before
36+
running this migration.
37+
"""
38+
raise RuntimeError(
39+
"This migration cannot be reversed. "
40+
"The speakers app and its data have been permanently removed. "
41+
"Restore from a backup if you need to recover."
42+
)
43+
44+
45+
class Migration(migrations.Migration):
46+
dependencies = [
47+
("core", "0009_auto_20160703_0857"),
48+
]
49+
50+
operations = [
51+
migrations.RunPython(
52+
drop_speakers_tables,
53+
reverse_drop_speakers_tables,
54+
),
55+
]

0 commit comments

Comments
 (0)