Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
fetch_initial_state: Avoid doing one db query per announcement stream.
Prakhar Pratyushf48e87cd3cb6
diff --git a/zerver/lib/events.py b/zerver/lib/events.pyindex 37ac40ce1a8..75fba6d7a73 100644--- a/zerver/lib/events.py+++ b/zerver/lib/events.py@@ -1681,8 +1681,11 @@ def do_events_register(event_types_set = None# Fetch the realm object again to prefetch all the- # group settings which support anonymous groups+ # settings that will be used in 'fetch_initial_state_data'# to avoid unnecessary DB queries.+ # The settings include:+ # * group settings which support anonymous groups+ # * announcements streamsrealm = get_realm_with_settings(realm_id=realm.id)if user_profile is None:diff --git a/zerver/models/realms.py b/zerver/models/realms.pyindex 4517f812ec9..4ed18c73628 100644--- a/zerver/models/realms.py+++ b/zerver/models/realms.py@@ -1102,10 +1102,12 @@ def get_realm_by_id(realm_id: int) -> Realm:def get_realm_with_settings(realm_id: int) -> Realm:- # Prefetch all the settings that can be set to anonymous groups.+ # Prefetch the following settings:+ # * All the settings that can be set to anonymous groups.# This also prefetches can_access_all_users_group setting,# even when it cannot be set to anonymous groups because# the setting is used when fetching users in the realm.+ # * Announcements streams.return Realm.objects.select_related("can_access_all_users_group","can_access_all_users_group__named_user_group",@@ -1117,6 +1119,9 @@ def get_realm_with_settings(realm_id: int) -> Realm:"direct_message_initiator_group__named_user_group","direct_message_permission_group","direct_message_permission_group__named_user_group",+ "new_stream_announcements_stream",+ "signup_announcements_stream",+ "zulip_update_announcements_stream",).get(id=realm_id)diff --git a/zerver/tests/test_event_system.py b/zerver/tests/test_event_system.pyindex 2d770963a1f..90e797daa39 100644--- a/zerver/tests/test_event_system.py+++ b/zerver/tests/test_event_system.py@@ -1174,7 +1174,7 @@ class FetchQueriesTest(ZulipTestCase):realm = get_realm_with_settings(realm_id=user.realm_id)with (- self.assert_database_query_count(43),+ self.assert_database_query_count(41),mock.patch("zerver.lib.events.always_want") as want_mock,):fetch_initial_state_data(user, realm=realm)@@ -1190,7 +1190,7 @@ class FetchQueriesTest(ZulipTestCase):muted_users=1,onboarding_steps=1,presence=1,- realm=3,+ realm=1,realm_bot=1,realm_domains=1,realm_embedded_bots=0,diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.pyindex e993b6f329c..05d8ba64cc5 100644--- a/zerver/tests/test_home.py+++ b/zerver/tests/test_home.py@@ -267,7 +267,7 @@ class HomeTest(ZulipTestCase):# Verify succeeds once logged-inwith (- self.assert_database_query_count(54),+ self.assert_database_query_count(52),patch("zerver.lib.cache.cache_set") as cache_mock,):result = self._get_home_page(stream="Denmark")@@ -572,7 +572,7 @@ class HomeTest(ZulipTestCase):# Verify number of queries for Realm admin isn't much higher than for normal users.self.login("iago")with (- self.assert_database_query_count(54),+ self.assert_database_query_count(52),patch("zerver.lib.cache.cache_set") as cache_mock,):result = self._get_home_page()@@ -604,7 +604,7 @@ class HomeTest(ZulipTestCase):self._get_home_page()# Then for the second page load, measure the number of queries.- with self.assert_database_query_count(49):+ with self.assert_database_query_count(47):result = self._get_home_page()# Do a sanity check that our new streams were in the payload.