Files
upstream-zulip/zerver/migrations/0770_recreate_missing_realmemoji.py
upstream-mirror 64898b2c79
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
2026-09-15 15:05:30 +05:00

98 lines
3.8 KiB
Python

from typing import Any
import orjson
from django.db import connection, migrations, transaction
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps
from django.db.models import Exists, OuterRef
ROLE_REALM_OWNER = 100
REALM_EMOJI_ADDED = 226
def recreate_missing_realmemoji(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
RealmEmoji = apps.get_model("zerver", "RealmEmoji")
RealmAuditLog = apps.get_model("zerver", "RealmAuditLog")
Realm = apps.get_model("zerver", "Realm")
UserProfile = apps.get_model("zerver", "UserProfile")
def get_first_human_owner_user(realm: Any) -> Any:
first_active_human_owner = UserProfile.objects.filter(
realm=realm, is_bot=False, role=ROLE_REALM_OWNER, is_active=True
).first()
if first_active_human_owner is not None:
return first_active_human_owner
fallback_owner_user = UserProfile.objects.filter(
realm=realm, is_bot=False, role=ROLE_REALM_OWNER, is_active=False
).first()
if fallback_owner_user is None:
raise AssertionError(f"No human owner users in realm {realm.string_id}")
return fallback_owner_user
emoji_add_log_queryset = RealmAuditLog.objects.filter(
realm=OuterRef("pk"),
event_type=REALM_EMOJI_ADDED,
)
for realm in (
Realm.objects.annotate(has_emoji_add_log=Exists(emoji_add_log_queryset))
.filter(has_emoji_add_log=True)
.iterator()
):
with transaction.atomic():
first_owner = None
sql = """
WITH distinct_emoji AS (
SELECT DISTINCT emoji.value AS e
FROM zerver_realmauditlog
CROSS JOIN LATERAL jsonb_each(
zerver_realmauditlog.extra_data->'realm_emoji'
) AS emoji(key, value)
WHERE zerver_realmauditlog.realm_id = %s
AND zerver_realmauditlog.event_type = %s
)
SELECT distinct_emoji.e FROM distinct_emoji
LEFT JOIN zerver_realmemoji
ON zerver_realmemoji.id = (e->>'id')::integer
OR (zerver_realmemoji.name = e->>'name'
AND zerver_realmemoji.realm_id = %s)
WHERE zerver_realmemoji.id IS NULL
"""
with connection.cursor() as cursor:
cursor.execute(sql, [realm.id, REALM_EMOJI_ADDED, realm.id])
for (raw_emoji,) in cursor.fetchall():
orphaned_emoji = orjson.loads(raw_emoji)
print(
f"Creating missing emoji {orphaned_emoji['id']} = {realm.string_id} / {orphaned_emoji['name']}"
)
file_name = orphaned_emoji["source_url"][
1 + orphaned_emoji["source_url"].rfind("/") :
]
if first_owner is None:
first_owner = get_first_human_owner_user(realm)
RealmEmoji.objects.create(
id=int(orphaned_emoji["id"]),
realm_id=realm.id,
name=orphaned_emoji["name"],
author_id=first_owner.id,
file_name=file_name,
deactivated=orphaned_emoji["deactivated"],
is_animated=orphaned_emoji["still_url"] is not None,
)
class Migration(migrations.Migration):
atomic = False
dependencies = [
("zerver", "0769_rename_profile_field_twitter_to_x"),
]
operations = [
migrations.RunPython(
recreate_missing_realmemoji,
elidable=True,
),
]