Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

migrations: Update direct message group migration to use direct SQL.

Alex Vandiver77b25e1a54fa
diff --git a/zerver/migrations/0790_backfill_update_recipient_type_of_personal_messages_to_dm_group.py b/zerver/migrations/0790_backfill_update_recipient_type_of_personal_messages_to_dm_group.py
index 9f14921e0b4..e4387b63b80 100644
--- a/zerver/migrations/0790_backfill_update_recipient_type_of_personal_messages_to_dm_group.py
+++ b/zerver/migrations/0790_backfill_update_recipient_type_of_personal_messages_to_dm_group.py
@@ -1,7 +1,6 @@
# Generated by Django 5.2.4 on 2025-08-17 00:11
import hashlib
-from typing import Any
from django.db import connection, migrations, transaction
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
@@ -32,49 +31,64 @@ def get_direct_message_group_hash(id_list: list[int]) -> str:
return hashlib.sha1(hash_key.encode()).hexdigest()
-def get_or_create_direct_message_group(apps: StateApps, id_list: list[int]) -> Any:
+def get_or_create_direct_message_group(apps: StateApps, id_list: list[int]) -> int:
"""
Takes a list of user IDs and returns the DirectMessageGroup object for
the group consisting of these users. If the DirectMessageGroup object
does not yet exist, it will be transparently created.
-
- This function is copied from zerver/model/recipients.py to avoid import issues.
"""
- DirectMessageGroup = apps.get_model("zerver", "DirectMessageGroup")
- Recipient = apps.get_model("zerver", "Recipient")
direct_message_group_hash = get_direct_message_group_hash(id_list)
- with transaction.atomic(savepoint=False):
- direct_message_group, created = DirectMessageGroup.objects.get_or_create(
- huddle_hash=direct_message_group_hash,
- group_size=len(id_list),
+ with transaction.atomic(savepoint=False), connection.cursor() as cursor:
+ # The "do update" is mostly superfluous here, but required so
+ # that we get the id back if the row already existed.
+ cursor.execute(
+ """
+ INSERT INTO zerver_huddle (huddle_hash, group_size)
+ VALUES (%s, %s)
+ ON CONFLICT (huddle_hash) DO UPDATE SET group_size = %s
+ RETURNING id, recipient_id
+ """,
+ [direct_message_group_hash, len(id_list), len(id_list)],
)
- if not created:
- return direct_message_group
+ direct_message_group_id, recipient_id = cursor.fetchone()
+ if recipient_id is not None:
+ return recipient_id
- recipient = Recipient.objects.create(
- type_id=direct_message_group.id, type=DIRECT_MESSAGE_GROUP_RECIPIENT
+ cursor.execute(
+ """
+ INSERT INTO zerver_recipient (type_id, type)
+ VALUES (%s, %s)
+ RETURNING id
+ """,
+ [direct_message_group_id, DIRECT_MESSAGE_GROUP_RECIPIENT],
)
- direct_message_group.recipient = recipient
- direct_message_group.save(update_fields=["recipient"])
- with connection.cursor() as cursor:
- cursor.execute(
- """
- INSERT INTO zerver_subscription
- (recipient_id, user_profile_id, is_user_active,
- active, is_muted, color, pin_to_top,
- desktop_notifications, audible_notifications,
- push_notifications, email_notifications,
- wildcard_mentions_notify)
- SELECT %s, id, is_active,
- true, false, '#c2c2c2', false,
- NULL, NULL, NULL, NULL, NULL
- FROM zerver_userprofile
- WHERE id IN %s
- """,
- [recipient.id, tuple(id_list)],
- )
- return direct_message_group
+ recipient_id = cursor.fetchone()[0]
+
+ cursor.execute(
+ """
+ UPDATE zerver_huddle SET recipient_id = %s WHERE id = %s
+ """,
+ [recipient_id, direct_message_group_id],
+ )
+
+ cursor.execute(
+ """
+ INSERT INTO zerver_subscription
+ (recipient_id, user_profile_id, is_user_active,
+ active, is_muted, color, pin_to_top,
+ desktop_notifications, audible_notifications,
+ push_notifications, email_notifications,
+ wildcard_mentions_notify)
+ SELECT %s, id, is_active,
+ true, false, '#c2c2c2', false,
+ NULL, NULL, NULL, NULL, NULL
+ FROM zerver_userprofile
+ WHERE id IN %s
+ """,
+ [recipient_id, tuple(id_list)],
+ )
+ return recipient_id
def backfill_update_recipient_type_of_personal_messages_to_dm_group(
@@ -135,14 +149,14 @@ def backfill_update_recipient_type_of_personal_messages_to_dm_group(
id_list = [sender_id, receiving_user_id]
with transaction.atomic():
- dmg = get_or_create_direct_message_group(apps, id_list)
+ group_recipient_id = get_or_create_direct_message_group(apps, id_list)
for table, col in tables_to_update:
cursor.execute(
SQL(
"UPDATE {} SET recipient_id = %s"
" WHERE {} = %s AND recipient_id = %s"
).format(Identifier(table), Identifier(col)),
- [dmg.recipient_id, sender_id, personal_rec_id],
+ [group_recipient_id, sender_id, personal_rec_id],
)
cursor.execute(