Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
do_delete_realm: Clean up orphan DirectMessageGroups.
Alex Vandiver2afa11b73a44
diff --git a/zerver/actions/realm_settings.py b/zerver/actions/realm_settings.pyindex a6eb0498295..a05523d215d 100644--- a/zerver/actions/realm_settings.py+++ b/zerver/actions/realm_settings.py@@ -37,6 +37,7 @@ from zerver.lib.utils import optional_bytes_to_mibfrom zerver.models import (ArchivedAttachment,Attachment,+ DirectMessageGroup,ImageAttachment,Message,NamedUserGroup,@@ -759,11 +760,37 @@ def do_delete_realm(realm: Realm) -> None:realm = Realm.objects.select_for_update(no_key=False).get(id=realm.id)do_delete_all_realm_attachments(realm)- # TODO: This approach leaks Recipient and DirectMessageGroup- # objects, because those don't have a foreign key to the Realm- # or any other model it cascades to (Realm/Stream/UserProfile/etc.).+ # realm.delete() leaves DirectMessageGroup and Recipient rows+ # behind (no FK back to Realm), and for DMGs with cross-realm+ # bot subs the bot Subscription survives too. Human-to-human+ # cross-realm DMs aren't currently possible, so every DMG our+ # users were in becomes orphan on realm.delete(): any survivor+ # must be a system bot. Snapshot the Recipients now, while+ # the Subscription join still exists.+ #+ # TODO: if cross-realm human DMs become possible, this has to+ # filter out DMGs that still have a non-bot subscriber from+ # another realm.+ orphan_dmg_recipient_ids = set(+ Subscription.objects.filter(+ recipient__type=Recipient.DIRECT_MESSAGE_GROUP,+ user_profile__realm_id=realm.id,+ )+ .distinct("recipient_id")+ .values_list("recipient_id", flat=True)+ )+ orphan_huddle_ids = set(+ Recipient.objects.filter(id__in=orphan_dmg_recipient_ids).values_list("type_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()+ DirectMessageGroup.objects.filter(id__in=orphan_huddle_ids).delete()+@transaction.atomic(durable=True)def do_scrub_realm(realm: Realm, *, acting_user: UserProfile | None) -> None:diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.pyindex 745417dbd3d..9b6ec1a68b3 100644--- a/zerver/tests/test_realm.py+++ b/zerver/tests/test_realm.py@@ -39,6 +39,7 @@ from zerver.actions.realm_settings import (do_change_realm_plan_type,do_deactivate_realm,do_delete_all_realm_attachments,+ do_delete_realm,do_reactivate_realm,do_scrub_realm,do_send_realm_reactivation_email,@@ -69,6 +70,7 @@ from zerver.lib.upload import upload_avatar_image, upload_message_attachmentfrom zerver.models import (Attachment,CustomProfileField,+ DirectMessageGroup,ImageAttachment,Message,NamedUserGroup,@@ -76,8 +78,10 @@ from zerver.models import (RealmAuditLog,RealmReactivationStatus,RealmUserDefault,+ Recipient,ScheduledEmail,Stream,+ Subscription,UserGroupMembership,UserMessage,UserProfile,@@ -85,6 +89,7 @@ from zerver.models import (from zerver.models.groups import SystemGroupsfrom zerver.models.realm_audit_logs import AuditLogEventTypefrom zerver.models.realms import get_realm+from zerver.models.recipients import get_or_create_direct_message_groupfrom zerver.models.scheduled_jobs import ScheduledMessagefrom zerver.models.streams import get_streamfrom zerver.models.users import get_system_bot, get_user_profile_by_id@@ -3707,3 +3712,49 @@ class ScrubRealmTest(ZulipTestCase):self.assertNotRegex(user.delivery_email, rf"^scrubbed-[a-z0-9]{{15}}@{re.escape(zulip.host)}$")+++class DeleteRealmTest(ZulipTestCase):+ def test_do_delete_realm_cleans_up_orphan_direct_message_groups(self) -> None:+ zulip = get_realm("zulip")+ internal_realm = get_realm(settings.SYSTEM_BOT_REALM)+ hamlet = self.example_user("hamlet")+ iago = self.example_user("iago")+ cordelia = self.lear_user("cordelia")+ king = self.lear_user("king")+ welcome_bot = get_system_bot(settings.WELCOME_BOT, internal_realm.id)++ # Orphan: realm user + system bot.+ bot_dmg = get_or_create_direct_message_group([hamlet.id, welcome_bot.id])+ # Orphan: realm users only.+ zulip_only_dmg = get_or_create_direct_message_group([hamlet.id, iago.id])+ # Preserved: in a different realm.+ lear_only_dmg = get_or_create_direct_message_group([cordelia.id, king.id])+ # Preserved: different realm's user + system bot.+ lear_bot_dmg = get_or_create_direct_message_group([cordelia.id, welcome_bot.id])+ assert bot_dmg.recipient_id is not None+ assert zulip_only_dmg.recipient_id is not None+ assert lear_bot_dmg.recipient_id is not None++ do_delete_realm(zulip)++ self.assertFalse(Realm.objects.filter(id=zulip.id).exists())++ self.assertFalse(DirectMessageGroup.objects.filter(id=bot_dmg.id).exists())+ self.assertFalse(Recipient.objects.filter(id=bot_dmg.recipient_id).exists())+ self.assertFalse(+ Subscription.objects.filter(+ user_profile=welcome_bot, recipient_id=bot_dmg.recipient_id+ ).exists()+ )+ self.assertFalse(DirectMessageGroup.objects.filter(id=zulip_only_dmg.id).exists())+ self.assertFalse(Recipient.objects.filter(id=zulip_only_dmg.recipient_id).exists())++ self.assertTrue(DirectMessageGroup.objects.filter(id=lear_only_dmg.id).exists())+ self.assertTrue(DirectMessageGroup.objects.filter(id=lear_bot_dmg.id).exists())+ self.assertTrue(+ Subscription.objects.filter(+ user_profile=welcome_bot, recipient_id=lear_bot_dmg.recipient_id+ ).exists()+ )+ self.assertTrue(UserProfile.objects.filter(id=welcome_bot.id).exists())