From f26d0affefdf7b9d88a75f51e13a98795ae02af8 Mon Sep 17 00:00:00 2001 From: Clara Dantas Date: Mon, 3 Aug 2020 14:55:57 -0300 Subject: [PATCH] bulk_update: Do updates in bulk where applicable. --- zerver/lib/bulk_create.py | 6 +++--- zerver/management/commands/deliver_scheduled_messages.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/zerver/lib/bulk_create.py b/zerver/lib/bulk_create.py index eb37044502..785afa8f4f 100644 --- a/zerver/lib/bulk_create.py +++ b/zerver/lib/bulk_create.py @@ -90,14 +90,14 @@ def bulk_set_users_or_streams_recipient_fields(model: Model, objects_dict = {obj.id: obj for obj in objects} + objects_to_update = set() for recipient in recipients: assert recipient.type == recipient_type result = objects_dict.get(recipient.type_id) if result is not None: result.recipient = recipient - # TODO: Django 2.2 has a bulk_update method, so once we manage to migrate to that version, - # we take adventage of this, instead of calling save individually. - result.save(update_fields=['recipient']) + objects_to_update.add(result) + model.objects.bulk_update(objects_to_update, ['recipient']) # This is only sed in populate_db, so doesn't really need tests def bulk_create_streams(realm: Realm, diff --git a/zerver/management/commands/deliver_scheduled_messages.py b/zerver/management/commands/deliver_scheduled_messages.py index f98a9ff9ef..bd50d3b8ba 100644 --- a/zerver/management/commands/deliver_scheduled_messages.py +++ b/zerver/management/commands/deliver_scheduled_messages.py @@ -65,7 +65,7 @@ Usage: ./manage.py deliver_scheduled_messages with transaction.atomic(): do_send_messages([self.construct_message(message)]) message.delivered = True - message.save(update_fields=['delivered']) + Message.objects.bulk_update(messages_to_deliver, ['delivered']) cur_time = timezone_now() time_next_min = (cur_time + timedelta(minutes=1)).replace(second=0, microsecond=0)