11from django .db import migrations
22from django .db .backends .postgresql .schema import DatabaseSchemaEditor
33from django .db .migrations .state import StateApps
4+ from django .db .models import OuterRef , Subquery
45
56
67def set_emoji_author (apps : StateApps , schema_editor : DatabaseSchemaEditor ) -> None :
@@ -13,20 +14,15 @@ def set_emoji_author(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> No
1314 UserProfile = apps .get_model ("zerver" , "UserProfile" )
1415 ROLE_REALM_OWNER = 100
1516
16- realm_emoji_to_update = []
17- for realm_emoji in RealmEmoji .objects .all ():
18- if realm_emoji .author_id is None :
19- user_profile = (
20- UserProfile .objects .filter (
21- realm_id = realm_emoji .realm_id , is_active = True , role = ROLE_REALM_OWNER
22- )
23- .order_by ("id" )
24- .first ()
17+ RealmEmoji .objects .filter (author = None ).update (
18+ author = Subquery (
19+ UserProfile .objects .filter (
20+ realm = OuterRef ("realm" ), is_active = True , role = ROLE_REALM_OWNER
2521 )
26- realm_emoji . author_id = user_profile . id
27- realm_emoji_to_update . append ( realm_emoji )
28-
29- RealmEmoji . objects . bulk_update ( realm_emoji_to_update , [ "author_id" ] )
22+ . order_by ( "id" )[: 1 ]
23+ . values ( "pk" )
24+ )
25+ )
3026
3127 # Previously, this also pushed `reupload_realm_emoji` events onto
3228 # the `deferred_work` queue; however,
0 commit comments