Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
export: Build UserMessage export dicts directly from cursor rows.
Alex Vandiver3969df28602c
diff --git a/zerver/lib/export.py b/zerver/lib/export.pyindex 2932ae489d9..4c7e81d3fda 100644--- a/zerver/lib/export.py+++ b/zerver/lib/export.py@@ -1722,17 +1722,43 @@ def fetch_usermessages(if export_full_with_consent:assert consented_user_ids is not Noneuser_profile_ids = consented_user_ids & user_profile_ids- user_message_query = (- UserMessage.objects.filter(user_profile_id__in=user_profile_ids, message_id__in=message_ids)- .order_by("id")- .iterator()- )++ # The hand-rolled SELECT below names UserMessage's columns+ # explicitly; this assert is to catch any future model column drift.+ assert {f.name for f in UserMessage._meta.concrete_fields} == {+ "id",+ "user_profile",+ "flags",+ "message",+ }, "UserMessage gained a field; update this SELECT and the yielded dict"++ # psycopg2 mogrifies an empty tuple to `IN ()`, which is a+ # PostgreSQL syntax error; Django's __in lookup short-circuits+ # an empty set via EmptyResultSet, and this matches that+ # behavior. EXPORT_FULL_WITH_CONSENT hits this whenever the+ # consent intersection is empty.+ if not user_profile_ids or not message_ids:+ return+logging.info("Fetching UserMessages for %s", message_filename)- for user_message in user_message_query:- user_message_obj = model_to_dict(user_message)- user_message_obj["flags_mask"] = user_message.flags.mask- del user_message_obj["flags"]- yield user_message_obj+ with connection.chunked_cursor() as cursor:+ cursor.execute(+ """+ SELECT id, user_profile_id, flags, message_id+ FROM zerver_usermessage+ WHERE user_profile_id IN %s+ AND message_id IN %s+ ORDER BY id+ """,+ [tuple(user_profile_ids), tuple(message_ids)],+ )+ for um_id, user_profile_id, flags, message_id in cursor:+ yield {+ "id": um_id,+ "user_profile": user_profile_id,+ "flags_mask": flags,+ "message": message_id,+ }def export_usermessages_batch(diff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.pyindex 4c96cecc37c..97275f28f28 100644--- a/zerver/tests/test_import_export.py+++ b/zerver/tests/test_import_export.py@@ -603,6 +603,21 @@ class RealmImportExportTest(ExportFile):self.assertEqual(exported_um["message"], um.message_id)self.assertEqual(exported_um["user_profile"], um.user_profile_id)+ # Verify flags_mask on every exported UserMessage equals the+ # int value BitHandler.mask returned for the model instance;+ # fetch_usermessages reads flags as a raw int from the cursor+ # rather than via the BitField wrapper, and this guards+ # against that substitution diverging.+ um_flags_by_id = dict(UserMessage.objects.values_list("id", "flags"))+ for exported_um in data["zerver_usermessage"]:+ self.assertEqual(exported_um["flags_mask"], um_flags_by_id[exported_um["id"]])+ # And confirm the set actually spans both zero and non-zero+ # values, so the loop above isn't vacuously checking only+ # flags=0 rows.+ flag_values = {r["flags_mask"] for r in data["zerver_usermessage"]}+ self.assertGreater(max(flag_values), 0)+ self.assertIn(0, flag_values)+exported_message = self.find_by_id(data["zerver_message"], um.message_id)self.assertEqual(exported_message["content"], um.message.content)