presence: Handle PresenceSequence in the export/import system.

This commit is contained in:
Mateusz Mandera 2024-05-26 02:38:57 +02:00 committed by Tim Abbott
parent 0dca8f2a38
commit b1d50b511c
4 changed files with 44 additions and 0 deletions

View File

@ -71,6 +71,7 @@ from zerver.models import (
UserStatus,
UserTopic,
)
from zerver.models.presence import PresenceSequence
from zerver.models.realms import get_realm
from zerver.models.users import get_system_bot, get_user_profile_by_id
@ -143,6 +144,7 @@ ALL_ZULIP_TABLES = {
"zerver_preregistrationrealm",
"zerver_preregistrationuser",
"zerver_preregistrationuser_streams",
"zerver_presencesequence",
"zerver_pushdevicetoken",
"zerver_reaction",
"zerver_realm",
@ -691,6 +693,13 @@ def get_realm_config() -> Config:
include_rows="realm_id__in",
)
Config(
table="zerver_presencesequence",
model=PresenceSequence,
normal_parent=realm_config,
include_rows="realm_id__in",
)
Config(
custom_tables=["zerver_scheduledmessage"],
virtual_parent=realm_config,

View File

@ -81,6 +81,7 @@ from zerver.models import (
UserTopic,
)
from zerver.models.groups import SystemGroups
from zerver.models.presence import PresenceSequence
from zerver.models.realms import get_realm
from zerver.models.recipients import get_huddle_hash
from zerver.models.users import get_system_bot, get_user_profile_by_id
@ -116,6 +117,7 @@ ID_MAP: Dict[str, Dict[int, int]] = {
"subscription": {},
"defaultstream": {},
"onboardingstep": {},
"presencesequence": {},
"reaction": {},
"realmauthenticationmethod": {},
"realmemoji": {},
@ -1063,6 +1065,8 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea
update_model_ids(UserProfile, data, "user_profile")
if "zerver_usergroup" in data:
update_model_ids(UserGroup, data, "usergroup")
if "zerver_presencesequence" in data:
update_model_ids(PresenceSequence, data, "presencesequence")
# Now we prepare to import the Realm table
re_map_foreign_keys(
@ -1099,6 +1103,13 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea
realm.save()
if "zerver_presencesequence" in data:
re_map_foreign_keys(data, "zerver_presencesequence", "realm", related_table="realm")
bulk_import_model(data, PresenceSequence)
else:
# We need to enforce the invariant that every realm must have a PresenceSequence.
PresenceSequence.objects.create(realm=realm, last_update_id=0)
if "zerver_usergroup" in data:
re_map_foreign_keys(data, "zerver_usergroup", "realm", related_table="realm")
bulk_import_model(data, UserGroup)

View File

@ -88,6 +88,7 @@ from zerver.models import (
)
from zerver.models.clients import get_client
from zerver.models.groups import SystemGroups
from zerver.models.presence import PresenceSequence
from zerver.models.realms import get_realm
from zerver.models.recipients import get_huddle_hash
from zerver.models.streams import get_active_streams, get_stream
@ -863,6 +864,12 @@ class RealmImportExportTest(ExportFile):
do_update_user_presence(
sample_user, client, timezone_now(), UserPresence.LEGACY_STATUS_ACTIVE_INT
)
user_presence_last_update_ids = set(
UserPresence.objects.filter(realm=original_realm)
.values_list("last_update_id", flat=True)
.distinct("last_update_id")
)
presence_sequence = PresenceSequence.objects.get(realm=original_realm)
# Set up scheduled messages.
ScheduledMessage.objects.filter(realm=original_realm).delete()
@ -1081,6 +1088,19 @@ class RealmImportExportTest(ExportFile):
# set to None due to being unexportable.
self.assertEqual(realmauditlog.acting_user, None)
# Verify the PresenceSequence for the realm got imported correctly.
imported_presence_sequence = PresenceSequence.objects.get(realm=imported_realm)
self.assertEqual(
presence_sequence.last_update_id, imported_presence_sequence.last_update_id
)
imported_last_update_ids = set(
UserPresence.objects.filter(realm=imported_realm)
.values_list("last_update_id", flat=True)
.distinct("last_update_id")
)
self.assertEqual(user_presence_last_update_ids, imported_last_update_ids)
self.assertEqual(imported_presence_sequence.last_update_id, max(imported_last_update_ids))
self.assertEqual(
Message.objects.filter(realm=original_realm).count(),
Message.objects.filter(realm=imported_realm).count(),

View File

@ -29,6 +29,7 @@ from zerver.lib.emoji import name_to_codepoint
from zerver.lib.import_realm import do_import_realm
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import Message, Reaction, Recipient, UserProfile
from zerver.models.presence import PresenceSequence
from zerver.models.realms import get_realm
from zerver.models.users import get_user
@ -744,6 +745,9 @@ class MatterMostImporter(ZulipTestCase):
realm = get_realm("gryffindor")
presence_sequence = PresenceSequence.objects.get(realm=realm)
self.assertEqual(presence_sequence.last_update_id, 0)
self.assertFalse(get_user("harry@zulip.com", realm).is_mirror_dummy)
self.assertFalse(get_user("ron@zulip.com", realm).is_mirror_dummy)
self.assertTrue(get_user("snape@zulip.com", realm).is_mirror_dummy)