Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
Revert "recipients: Reduce query count for new DirectMessageGroup."
Mateusz Mandera25a08ae420fe
diff --git a/zerver/models/recipients.py b/zerver/models/recipients.pyindex 9e03180fa54..1ef1ca13593 100644--- a/zerver/models/recipients.py+++ b/zerver/models/recipients.py@@ -2,9 +2,8 @@ import hashlibfrom collections import defaultdictfrom typing import TYPE_CHECKING-from django.db import connection, models, transaction+from django.db import models, transactionfrom django.db.models import QuerySet-from psycopg2.sql import SQL, Identifierfrom typing_extensions import overridefrom zerver.lib.display_recipient import get_display_recipient@@ -157,69 +156,23 @@ def get_or_create_direct_message_group(id_list: list[int]) -> DirectMessageGrouphuddle_hash=direct_message_group_hash,group_size=len(id_list),)-- if not created:- return direct_message_group-- recipient = Recipient.objects.create(- type_id=direct_message_group.id, type=Recipient.DIRECT_MESSAGE_GROUP- )-- # The equivalent effect of doing- # direct_message_group.save(update_fields=["recipient"]) is handled- # in the following raw query via UPDATE to save us a query.- direct_message_group.recipient = recipient-- # In a single raw query, we do 2 required things after creating- # a new DirectMessageGroup object:- # 1. Update the recipient field to reflect the above assignment in db.- # 2. Bulk create subscriptions for the participants.- query = SQL(- """- UPDATE {dmg_table}- SET recipient_id = %(recipient_id)s- WHERE {dmg_table}.id = %(dmg_id)s;-- INSERT INTO {subscription_table}- (- user_profile_id,- is_user_active,- recipient_id,- active,- is_muted,- color,- pin_to_top- )- SELECT- {user_profile_table}.id,- {user_profile_table}.is_active,- %(recipient_id)s,- %(active)s,- %(is_muted)s,- %(color)s,- %(pin_to_top)s- FROM {user_profile_table}- WHERE {user_profile_table}.id = ANY(%(participant_ids)s);- """- ).format(- dmg_table=Identifier(DirectMessageGroup._meta.db_table),- subscription_table=Identifier(Subscription._meta.db_table),- user_profile_table=Identifier(UserProfile._meta.db_table),- )- with connection.cursor() as cursor:- cursor.execute(- query,- {- "recipient_id": recipient.id,- "dmg_id": direct_message_group.id,- "active": Subscription._meta.get_field("active").get_default(),- "is_muted": Subscription._meta.get_field("is_muted").get_default(),- "color": Subscription._meta.get_field("color").get_default(),- "pin_to_top": Subscription._meta.get_field("pin_to_top").get_default(),- "participant_ids": id_list,- },+ if created:+ recipient = Recipient.objects.create(+ type_id=direct_message_group.id, type=Recipient.DIRECT_MESSAGE_GROUP)-+ direct_message_group.recipient = recipient+ direct_message_group.save(update_fields=["recipient"])+ subs_to_create = [+ Subscription(+ recipient=recipient,+ user_profile_id=user_profile_id,+ is_user_active=is_active,+ )+ for user_profile_id, is_active in UserProfile.objects.filter(id__in=id_list)+ .distinct("id")+ .values_list("id", "is_active")+ ]+ Subscription.objects.bulk_create(subs_to_create)return direct_message_groupdiff --git a/zerver/tests/test_channel_fetch.py b/zerver/tests/test_channel_fetch.pyindex 4bf69240627..ba394ce8dae 100644--- a/zerver/tests/test_channel_fetch.py+++ b/zerver/tests/test_channel_fetch.py@@ -883,7 +883,7 @@ class GetSubscribersTest(ZulipTestCase):polonius.id,]- with self.assert_database_query_count(61):+ with self.assert_database_query_count(67):self.subscribe_via_post(self.user_profile,stream_names,diff --git a/zerver/tests/test_example.py b/zerver/tests/test_example.pyindex d13d3d41859..2be2e167668 100644--- a/zerver/tests/test_example.py+++ b/zerver/tests/test_example.py@@ -365,7 +365,7 @@ class TestQueryCounts(ZulipTestCase):cordelia = self.example_user("cordelia")# when direct message group doesn't exist and should be created as part of the flow- with self.assert_database_query_count(20):+ with self.assert_database_query_count(22):self.send_personal_message(from_user=hamlet,to_user=cordelia,diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.pyindex c28e7c0ae63..c3045f438cf 100644--- a/zerver/tests/test_message_send.py+++ b/zerver/tests/test_message_send.py@@ -2950,7 +2950,7 @@ class PersonalMessageSendTest(ZulipTestCase):prospero = self.example_user("prospero")# A normal user sends a personal message.- with self.assert_database_query_count(20):+ with self.assert_database_query_count(22):self.send_personal_message(hamlet, cordelia)# Give guests limited user access.@@ -2961,12 +2961,12 @@ class PersonalMessageSendTest(ZulipTestCase):# A guest with limited user access sends a personal message# to another accessible user.- with self.assert_database_query_count(21):+ with self.assert_database_query_count(23):self.send_personal_message(polonius, hamlet)# A guest with limited user access sends a personal message# to themself.- with self.assert_database_query_count(17):+ with self.assert_database_query_count(19):self.send_personal_message(polonius, polonius)# A guest with limited user access sends a personal message@@ -2999,7 +2999,7 @@ class PersonalMessageSendTest(ZulipTestCase):# A normal user sends the first message# to a new DirectMessageGroup.- with self.assert_database_query_count(24):+ with self.assert_database_query_count(26):self.send_group_direct_message(iago, recipients)# A normal user sends a message@@ -3018,7 +3018,7 @@ class PersonalMessageSendTest(ZulipTestCase):# A guest with limited user access sends the first message# to a new DirectMessageGroup.- with self.assert_database_query_count(26):+ with self.assert_database_query_count(28):self.send_group_direct_message(polonius, recipients)# A guest with limited user access sends a message@@ -3116,7 +3116,7 @@ class PersonalMessageSendTest(ZulipTestCase):acting_user=None,)othello = self.example_user("othello")- with self.assert_database_query_count(19):+ with self.assert_database_query_count(21):self.send_personal_message(user_profile, othello)def test_direct_message_permission_group_setting(self) -> None:@@ -3144,7 +3144,7 @@ class PersonalMessageSendTest(ZulipTestCase):acting_user=None,)# Tests if the user is allowed to send to administrators.- with self.assert_database_query_count(20):+ with self.assert_database_query_count(22):self.send_personal_message(user_profile, admin)self.send_personal_message(admin, user_profile)# Tests if we can send messages to self irrespective of the value of the setting.@@ -3162,7 +3162,7 @@ class PersonalMessageSendTest(ZulipTestCase):# We can send to this direct message group as it has administrator as one of the# recipient.- with self.assert_database_query_count(20):+ with self.assert_database_query_count(22):self.send_group_direct_message(user_profile, direct_message_group)self.send_group_direct_message(admin, direct_message_group)diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.pyindex efb174b626d..1e3eca46228 100644--- a/zerver/tests/test_signup.py+++ b/zerver/tests/test_signup.py@@ -1068,7 +1068,7 @@ class LoginTest(ZulipTestCase):# to sending messages, such as getting the welcome bot, looking up# the alert words for a realm, etc.with (- self.assert_database_query_count(97),+ self.assert_database_query_count(99),self.assert_memcached_count(19),self.captureOnCommitCallbacks(execute=True),):diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex da160ad123f..cb109788723 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -1154,7 +1154,7 @@ class QueryCountTest(ZulipTestCase):prereg_user = PreregistrationUser.objects.get(email="[email protected]")with (- self.assert_database_query_count(91),+ self.assert_database_query_count(95),self.assert_memcached_count(24),self.capture_send_event_calls(expected_num_events=11) as events,):