populate_db: Pass realm as arg to select_related calls.

This commit updates the select_related calls in queries to
get UserProfile objects in populate_db code to pass "realm"
as argument to select_related call.

Also, note that "realm" is the only non-null foreign key field
in UserProfile object, so select_related() was only fetching
realm object previously as well. But we should still pass "realm"
as argument in select_related call so that we can make sure that
only required fields are selected in case we add more foreign
keys to UserProfile in future.
This commit is contained in:
Sahil Batra 2023-07-12 15:31:22 +05:30 committed by Tim Abbott
parent c0029319f9
commit 62f01edee3
1 changed files with 2 additions and 2 deletions

View File

@ -132,7 +132,7 @@ def subscribe_users_to_streams(realm: Realm, stream_dict: Dict[str, Dict[str, An
subscriptions_to_add = [] subscriptions_to_add = []
event_time = timezone_now() event_time = timezone_now()
all_subscription_logs = [] all_subscription_logs = []
profiles = UserProfile.objects.select_related().filter(realm=realm) profiles = UserProfile.objects.select_related("realm").filter(realm=realm)
for i, stream_name in enumerate(stream_dict): for i, stream_name in enumerate(stream_dict):
stream = Stream.objects.get(name=stream_name, realm=realm) stream = Stream.objects.get(name=stream_name, realm=realm)
recipient = Recipient.objects.get(type=Recipient.STREAM, type_id=stream.id) recipient = Recipient.objects.get(type=Recipient.STREAM, type_id=stream.id)
@ -621,7 +621,7 @@ class Command(BaseCommand):
subscriptions_list: List[Tuple[UserProfile, Recipient]] = [] subscriptions_list: List[Tuple[UserProfile, Recipient]] = []
profiles: Sequence[UserProfile] = list( profiles: Sequence[UserProfile] = list(
UserProfile.objects.select_related().filter(is_bot=False).order_by("email") UserProfile.objects.select_related("realm").filter(is_bot=False).order_by("email")
) )
if options["test_suite"]: if options["test_suite"]: