2022-03-05 16:01:19 +01:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
from django.db import migrations, models
|
2023-03-04 01:40:40 +01:00
|
|
|
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
2022-03-05 16:01:19 +01:00
|
|
|
from django.db.migrations.state import StateApps
|
|
|
|
|
|
|
|
|
2022-05-27 23:33:51 +02:00
|
|
|
def backfill_user_profile_uuid(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
|
2022-03-05 16:01:19 +01:00
|
|
|
UserProfile = apps.get_model("zerver", "UserProfile")
|
|
|
|
|
|
|
|
max_id = UserProfile.objects.aggregate(models.Max("id"))["id__max"]
|
|
|
|
if max_id is None:
|
|
|
|
# Nothing to do if there are no users yet.
|
|
|
|
return
|
|
|
|
|
|
|
|
BATCH_SIZE = 10000
|
|
|
|
lower_bound = 0
|
|
|
|
|
|
|
|
while lower_bound < max_id:
|
|
|
|
user_profiles_to_update = []
|
|
|
|
for user_profile in UserProfile.objects.filter(
|
|
|
|
id__gt=lower_bound, id__lte=lower_bound + BATCH_SIZE, uuid=None
|
|
|
|
).only("id", "uuid"):
|
|
|
|
user_profile.uuid = uuid.uuid4()
|
|
|
|
user_profiles_to_update.append(user_profile)
|
|
|
|
lower_bound += BATCH_SIZE
|
|
|
|
|
|
|
|
UserProfile.objects.bulk_update(user_profiles_to_update, ["uuid"])
|
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
atomic = False
|
|
|
|
|
|
|
|
dependencies = [
|
|
|
|
("zerver", "0379_userprofile_uuid"),
|
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.RunPython(backfill_user_profile_uuid, reverse_code=migrations.RunPython.noop),
|
|
|
|
]
|