snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
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

This commit is contained in:
upstream-mirror
2026-09-15 15:05:30 +05:00
commit 64898b2c79
8999 changed files with 1757358 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
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
from django.db.models import Exists, OuterRef
STREAM = 2
DIRECT_MESSAGE_GROUP = 3
BATCH_SIZE = 1000
def delete_orphan_recipients_and_huddles(
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
) -> None:
Recipient = apps.get_model("zerver", "Recipient")
Subscription = apps.get_model("zerver", "Subscription")
DirectMessageGroup = apps.get_model("zerver", "DirectMessageGroup")
UserProfile = apps.get_model("zerver", "UserProfile")
Stream = apps.get_model("zerver", "Stream")
# Stream has no FK from Recipient (only a type_id int); when
# realm.delete() cascades the Stream, its Recipient row is left
# behind. Orphans are those whose type_id no longer points at
# a live Stream.
orphan_stream_recipient_ids = list(
Recipient.objects.filter(type=STREAM)
.exclude(type_id__in=Stream.objects.values_list("id", flat=True))
.values_list("id", flat=True)
)
print()
print(f"Deleting {len(orphan_stream_recipient_ids)} orphan stream recipients...")
for chunk_start in range(0, len(orphan_stream_recipient_ids), BATCH_SIZE):
chunk = orphan_stream_recipient_ids[chunk_start : chunk_start + BATCH_SIZE]
Recipient.objects.filter(id__in=chunk).delete()
print(
f" Deleted {chunk_start + len(chunk)}/{len(orphan_stream_recipient_ids)} orphan stream recipients."
)
# A DMG Recipient is orphan when its only remaining Subscriptions
# are from cross-realm system bots, or when none remain.
crossrealm_bot_ids = set(
UserProfile.objects.filter(
delivery_email__in=settings.CROSS_REALM_BOT_EMAILS,
).values_list("id", flat=True)
)
has_human_subscriber = Exists(
Subscription.objects.filter(recipient_id=OuterRef("id")).exclude(
user_profile_id__in=crossrealm_bot_ids,
)
)
orphan_dmg_recipient_ids = list(
Recipient.objects.filter(type=DIRECT_MESSAGE_GROUP)
.annotate(has_human=has_human_subscriber)
.filter(has_human=False)
.values_list("id", flat=True)
)
print(f"Deleting {len(orphan_dmg_recipient_ids)} orphan direct message groups...")
for chunk_start in range(0, len(orphan_dmg_recipient_ids), BATCH_SIZE):
chunk = orphan_dmg_recipient_ids[chunk_start : chunk_start + BATCH_SIZE]
chunk_huddle_ids = list(
Recipient.objects.filter(id__in=chunk).values_list("type_id", flat=True)
)
# Recipient.delete() cascades Subscription and Message;
# DirectMessageGroup.recipient is SET_NULL, so the DMG row
# needs an explicit delete.
Recipient.objects.filter(id__in=chunk).delete()
DirectMessageGroup.objects.filter(id__in=chunk_huddle_ids).delete()
print(
f" Deleted {chunk_start + len(chunk)}/{len(orphan_dmg_recipient_ids)} orphan direct message groups."
)
class Migration(migrations.Migration):
atomic = False
dependencies = [
("zerver", "0802_realmexport_restore_command_line_export_status"),
]
operations = [
migrations.RunPython(
delete_orphan_recipients_and_huddles,
reverse_code=migrations.RunPython.noop,
elidable=True,
),
]