migration: Rename 'populate_db' Client to 'ZulipDataImport'.

This commit:

* Creates a migration to rename any existing Client with
name="populate_db" to "ZulipDataImport".
* Updates populate_db.py to use ZulipDataImport for new
message creation

These changes should make code to identify imported messages
considerably more readable.

Fixes #33909.
This commit is contained in:
userAdityaa
2025-04-15 22:46:56 +05:30
committed by Tim Abbott
parent 9577dc9da7
commit 354a16fb0a
6 changed files with 50 additions and 10 deletions

View File

@@ -38,6 +38,17 @@ from zproject.backends import all_default_backend_names
# stubs
ZerverFieldsT: TypeAlias = dict[str, Any]
DATA_IMPORT_CLIENTS = {
# Match low ID clients in zerver/lib/server_initialization.py.
# This has no functional impact other than ensuring low IDs.
"Internal": 1,
"website": 2,
"ZulipMobile": 3,
"ZulipElectron": 4,
# Special client key to be used for data import messages.
"ZulipDataImport": 5,
}
class SubscriberHandler:
def __init__(self) -> None:
@@ -352,9 +363,8 @@ def build_realm(
) -> ZerverFieldsT:
realm = dict(
zerver_client=[
{"name": "populate_db", "id": 1},
{"name": "website", "id": 2},
{"name": "API", "id": 3},
{"name": client_name, "id": client_id}
for client_name, client_id in DATA_IMPORT_CLIENTS.items()
],
zerver_customprofilefield=[],
zerver_customprofilefieldvalue=[],
@@ -520,7 +530,7 @@ def build_message(
zulip_message, exclude=["recipient", "sender", "sending_client"]
)
zulip_message_dict["sender"] = user_id
zulip_message_dict["sending_client"] = 1
zulip_message_dict["sending_client"] = DATA_IMPORT_CLIENTS["ZulipDataImport"]
zulip_message_dict["recipient"] = recipient_id
zulip_message_dict["date_sent"] = date_sent

View File

@@ -56,6 +56,8 @@ def create_internal_realm() -> None:
# just ensures these get low IDs in production, and in development
# avoids an extra database write for the first HTTP request in
# most tests.
#
# These are currently also present in DATA_IMPORT_CLIENTS.
get_client("Internal")
get_client("website")
get_client("ZulipMobile")

View File

@@ -0,0 +1,27 @@
from django.db import migrations
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps
def rename_populate_db_client(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
"""Renames the 'populate_db' client to 'ZulipDataImport' to
better reflect its use in production, which is messages imported
into Zulip via official data import tools.
"""
Client = apps.get_model("zerver", "Client")
Client.objects.filter(name="populate_db").update(name="ZulipDataImport")
class Migration(migrations.Migration):
dependencies = [
("zerver", "0701_merge"),
]
operations = [
migrations.RunPython(
rename_populate_db_client,
reverse_code=migrations.RunPython.noop,
elidable=True,
),
]

View File

@@ -7494,7 +7494,7 @@ paths:
"sender_full_name": "King Hamlet",
"recipient_id": 27,
"topic_links": [],
"client": "populate_db",
"client": "ZulipDataImport",
"avatar_url": "https://secure.gravatar.com/avatar/6d8cad0fd00256e7b40691d27ddfd466?d=identicon&version=1",
"submessages": [],
"sender_email": "hamlet@zulip.com",
@@ -7516,7 +7516,7 @@ paths:
"sender_full_name": "King Hamlet",
"recipient_id": 20,
"topic_links": [],
"client": "populate_db",
"client": "ZulipDataImport",
"avatar_url": "https://secure.gravatar.com/avatar/6d8cad0fd00256e7b40691d27ddfd466?d=identicon&version=1",
"submessages": [],
"sender_email": "hamlet@zulip.com",
@@ -8846,7 +8846,7 @@ paths:
"sender_full_name": "King Hamlet",
"recipient_id": 27,
"topic_links": [],
"client": "populate_db",
"client": "ZulipDataImport",
"avatar_url": "https://secure.gravatar.com/avatar/6d8cad0fd00256e7b40691d27ddfd466?d=identicon&version=1",
"submessages": [],
"sender_email": "hamlet@zulip.com",

View File

@@ -1017,6 +1017,7 @@ class SlackImporter(ZulipTestCase):
) = slack_workspace_to_realm(
"testdomain", realm_id, user_list, "test-realm", "./random_path", {}
)
test_zerver_realmdomain = [
{"realm": realm_id, "allow_subdomains": False, "domain": "testdomain", "id": realm_id}
]

View File

@@ -836,7 +836,7 @@ class Command(ZulipBaseCommand):
# cURL example to delete an existing scheduled message.
check_schedule_message(
sender=iago,
client=get_client("populate_db"),
client=get_client("ZulipDataImport"),
recipient_type_name="stream",
message_to=[Stream.objects.get(name="Denmark", realm=zulip_realm).id],
topic_name="test-api",
@@ -846,7 +846,7 @@ class Command(ZulipBaseCommand):
)
check_schedule_message(
sender=iago,
client=get_client("populate_db"),
client=get_client("ZulipDataImport"),
recipient_type_name="private",
message_to=[iago.id],
topic_name=None,
@@ -1242,7 +1242,7 @@ def generate_and_send_messages(
while num_messages < tot_messages:
saved_data: dict[str, Any] = {}
message = Message(realm=realm)
message.sending_client = get_client("populate_db")
message.sending_client = get_client("ZulipDataImport")
message.content = next(texts)