Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

do_delete_realm: Clean up orphan Stream Recipients too.

Alex Vandivere5e3ebce1347
diff --git a/zerver/actions/realm_settings.py b/zerver/actions/realm_settings.py
index a05523d215d..27fcc7acdf1 100644
--- a/zerver/actions/realm_settings.py
+++ b/zerver/actions/realm_settings.py
@@ -783,12 +783,21 @@ def do_delete_realm(realm: Realm) -> None:
Recipient.objects.filter(id__in=orphan_dmg_recipient_ids).values_list("type_id", flat=True)
)
+ # Stream Recipients have no FK back to Stream either, so the
+ # realm's Stream cascade strands them in the same way. Capture
+ # them alongside the DMG Recipients.
+ orphan_stream_recipient_ids = set(
+ Stream.objects.filter(realm=realm)
+ .exclude(recipient__isnull=True)
+ .values_list("recipient_id", flat=True)
+ )
+
realm.delete()
# Recipient.delete() cascades Subscription and Message;
# DirectMessageGroup.recipient is SET_NULL, so the DMG row
# needs an explicit delete.
- Recipient.objects.filter(id__in=orphan_dmg_recipient_ids).delete()
+ Recipient.objects.filter(id__in=orphan_dmg_recipient_ids | orphan_stream_recipient_ids).delete()
DirectMessageGroup.objects.filter(id__in=orphan_huddle_ids).delete()
diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py
index 9b6ec1a68b3..6d6271588b9 100644
--- a/zerver/tests/test_realm.py
+++ b/zerver/tests/test_realm.py
@@ -3715,7 +3715,7 @@ class ScrubRealmTest(ZulipTestCase):
class DeleteRealmTest(ZulipTestCase):
- def test_do_delete_realm_cleans_up_orphan_direct_message_groups(self) -> None:
+ def test_do_delete_realm_cleans_up_orphan_recipients(self) -> None:
zulip = get_realm("zulip")
internal_realm = get_realm(settings.SYSTEM_BOT_REALM)
hamlet = self.example_user("hamlet")
@@ -3736,6 +3736,15 @@ class DeleteRealmTest(ZulipTestCase):
assert zulip_only_dmg.recipient_id is not None
assert lear_bot_dmg.recipient_id is not None
+ zulip_stream_recipient_ids = set(
+ Stream.objects.filter(realm=zulip).values_list("recipient_id", flat=True)
+ )
+ lear_stream_recipient_ids = set(
+ Stream.objects.filter(realm=get_realm("lear")).values_list("recipient_id", flat=True)
+ )
+ self.assertNotEqual(zulip_stream_recipient_ids, set())
+ self.assertNotEqual(lear_stream_recipient_ids, set())
+
do_delete_realm(zulip)
self.assertFalse(Realm.objects.filter(id=zulip.id).exists())
@@ -3757,4 +3766,15 @@ class DeleteRealmTest(ZulipTestCase):
user_profile=welcome_bot, recipient_id=lear_bot_dmg.recipient_id
).exists()
)
+
+ self.assertFalse(Recipient.objects.filter(id__in=zulip_stream_recipient_ids).exists())
+ self.assertEqual(
+ set(
+ Recipient.objects.filter(id__in=lear_stream_recipient_ids).values_list(
+ "id", flat=True
+ )
+ ),
+ lear_stream_recipient_ids,
+ )
+
self.assertTrue(UserProfile.objects.filter(id=welcome_bot.id).exists())