user_message: Use INSERT ... ON CONFLICT for historical UM creation.

Rather than use a bulk insert via Django, use the faster
`bulk_insert_all_ums` that we already have.  This also adds a `ON
CONFLICT` clause, to make the insert resilient to race conditions.

There are currently two callsites, with different desired `ON
CONFLICT` behaviours:
 - For `notify_reaction_update`, if the `UserMessage` had already been
   created, we would have done nothing to change it.
 - For `do_update_message_flags`, we would have ensured a specific bit
   was (un)set.

Extend `create_historical_user_messages` and `bulk_insert_all_ums` to
support `ON CONFLICT (...) UPDATE SET flags = ...`.
This commit is contained in:
Alex Vandiver
2024-03-26 14:24:45 +00:00
committed by Tim Abbott
parent 52e3c8e1b2
commit 7988aad159
3 changed files with 122 additions and 13 deletions

View File

@@ -338,8 +338,9 @@ def do_update_message_flags(
create_historical_user_messages(
user_id=user_profile.id,
message_ids=historical_message_ids,
flags=(DEFAULT_HISTORICAL_FLAGS & ~flagattr) | flag_target,
message_ids=list(historical_message_ids),
flagattr=flagattr,
flag_target=flag_target,
)
to_update = UserMessage.objects.filter(