Files
upstream-zulip/zerver/migrations/0672_fix_attachment_realm.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

84 lines
3.3 KiB
Python

import re
from django.conf import settings
from django.db import migrations
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps
def fix_attachment_realm(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
Realm = apps.get_model("zerver", "Realm")
Attachment = apps.get_model("zerver", "Attachment")
ArchivedAttachment = apps.get_model("zerver", "ArchivedAttachment")
if not Realm.objects.exists():
return
internal_realm = Realm.objects.get(string_id=settings.SYSTEM_BOT_REALM)
print()
for model in [Attachment, ArchivedAttachment]:
for attachment in model.objects.filter(realm_id=internal_realm.id).order_by("id"):
message = attachment.messages.only("realm_id").first()
if message is not None:
realm_id = message.realm_id
else:
# If attachment.messages is empty, try to infer the realm from path_id.
# The s3 backend set the path_id based on the correct realm, while the local
# storage backend formed path_id based on sender's realm (which was wrong
# for these attachments we're fixing, since the sender was a cross realm bot).
# We don't need to complicate the logic here with conditioning on the backend
# because worst case scenario, this just infers the realm to be the system bot
# realm, thus not changing anything at all.
matches = re.findall(r"^(\d+)\/", attachment.path_id)
if not matches:
print(
f"No realm_id found in path_id '{attachment.path_id}' of attachment {attachment.id}"
)
continue
try:
realm_id = int(matches[0])
if not Realm.objects.filter(id=realm_id).exists():
# If the realm doesn't exist (e.g. due to deletion), we can't do anything.
continue
except ValueError:
# Don't do anything if path_id doesn't start with a sensible realm id.
print(
f"Encountered ValueError for realm_id {realm_id} inferred from path_id of attachment {attachment.id}"
)
continue
if realm_id == attachment.realm_id:
# It's already correct, nothing to do.
continue
print(
f"Fixing incorrect realm for {model.__name__} {attachment.id} ({attachment.realm_id} => {realm_id})"
)
attachment.realm_id = realm_id
attachment.save(update_fields=["realm_id"])
class Migration(migrations.Migration):
"""
Old messages with attachments, sent by cross-realm bots, didn't have
the realm set correctly to the target realm, this migration fixes it
by looking at the messages linking to the attachment and setting the realm
based on the recipient.
"""
atomic = False
dependencies = [
("zerver", "0671_remove_realm_wildcard_mention_policy"),
]
operations = [
migrations.RunPython(
fix_attachment_realm,
reverse_code=migrations.RunPython.noop,
elidable=True,
)
]