migrations: Fix 0511 to backfill in SQL rather than Python loops.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-09-24 18:23:24 -07:00
parent 76a602842d
commit 850baaa368
1 changed files with 20 additions and 11 deletions

View File

@ -5,6 +5,7 @@ from django.conf import settings
from django.db import migrations, models from django.db import migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps from django.db.migrations.state import StateApps
from django.db.models import F, OuterRef, Subquery
def backfill_creator_id_from_realm_audit_log( def backfill_creator_id_from_realm_audit_log(
@ -14,17 +15,25 @@ def backfill_creator_id_from_realm_audit_log(
RealmAuditLog.STREAM_CREATED = 601 RealmAuditLog.STREAM_CREATED = 601
Stream = apps.get_model("zerver", "Stream") Stream = apps.get_model("zerver", "Stream")
stream_updates = [] batch_size = 10000
for audit_log_entry in RealmAuditLog.objects.select_related("modified_stream").filter( try:
event_type=RealmAuditLog.STREAM_CREATED, for id_start in range(
acting_user_id__isnull=False, RealmAuditLog.objects.earliest("id").id,
): RealmAuditLog.objects.latest("id").id + 1,
assert audit_log_entry.modified_stream is not None batch_size,
stream = audit_log_entry.modified_stream ):
stream.creator_id = audit_log_entry.acting_user_id Stream.objects.annotate(
stream_updates.append(stream) new_creator=Subquery(
RealmAuditLog.objects.filter(
Stream.objects.bulk_update(stream_updates, ["creator_id"], batch_size=1000) id__range=(id_start, id_start + batch_size - 1),
event_type=RealmAuditLog.STREAM_CREATED,
acting_user__isnull=False,
modified_stream=OuterRef("pk"),
).values("acting_user")
)
).filter(creator=None, new_creator__isnull=False).update(creator=F("new_creator"))
except RealmAuditLog.DoesNotExist:
pass
class Migration(migrations.Migration): class Migration(migrations.Migration):