mirror of https://github.com/zulip/zulip.git
migrations: Fix 0584 to backfill in SQL rather than Python loops.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
parent
850baaa368
commit
37a77ed7d7
|
@ -3,6 +3,7 @@
|
||||||
from django.db import migrations
|
from django.db import migrations
|
||||||
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_and_date_created_from_realm_audit_log(
|
def backfill_creator_id_and_date_created_from_realm_audit_log(
|
||||||
|
@ -12,31 +13,27 @@ def backfill_creator_id_and_date_created_from_realm_audit_log(
|
||||||
RealmAuditLog.USER_GROUP_CREATED = 701
|
RealmAuditLog.USER_GROUP_CREATED = 701
|
||||||
NamedUserGroup = apps.get_model("zerver", "NamedUserGroup")
|
NamedUserGroup = apps.get_model("zerver", "NamedUserGroup")
|
||||||
|
|
||||||
user_group_creator_updates = []
|
batch_size = 10000
|
||||||
for audit_log_entry in RealmAuditLog.objects.select_related("modified_user_group").filter(
|
try:
|
||||||
event_type=RealmAuditLog.USER_GROUP_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_user_group is not None
|
batch_size,
|
||||||
user_group = audit_log_entry.modified_user_group
|
):
|
||||||
user_group.creator_id = audit_log_entry.acting_user_id
|
creation_entry = RealmAuditLog.objects.filter(
|
||||||
user_group_creator_updates.append(user_group)
|
id__range=(id_start, id_start + batch_size - 1),
|
||||||
|
event_type=RealmAuditLog.USER_GROUP_CREATED,
|
||||||
NamedUserGroup.objects.bulk_update(user_group_creator_updates, ["creator_id"], batch_size=1000)
|
acting_user__isnull=False,
|
||||||
|
modified_user_group=OuterRef("pk"),
|
||||||
user_group_date_created_updates = []
|
)
|
||||||
for audit_log_entry in RealmAuditLog.objects.select_related("modified_user_group").filter(
|
NamedUserGroup.objects.annotate(
|
||||||
event_type=RealmAuditLog.USER_GROUP_CREATED,
|
new_creator=Subquery(creation_entry.values("acting_user")),
|
||||||
event_time__isnull=False,
|
new_date_created=Subquery(creation_entry.values("event_time")),
|
||||||
):
|
).filter(date_created=None, new_date_created__isnull=False).update(
|
||||||
assert audit_log_entry.modified_user_group is not None
|
creator=F("new_creator"), date_created=F("new_date_created")
|
||||||
user_group = audit_log_entry.modified_user_group
|
)
|
||||||
user_group.date_created = audit_log_entry.event_time
|
except RealmAuditLog.DoesNotExist:
|
||||||
user_group_date_created_updates.append(user_group)
|
pass
|
||||||
|
|
||||||
NamedUserGroup.objects.bulk_update(
|
|
||||||
user_group_date_created_updates, ["date_created"], batch_size=1000
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
Loading…
Reference in New Issue