Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
models: Add UserProfile.uuid field and backfill migrations.
Mateusz Mandera3e2ad84bbe7c
diff --git a/zerver/migrations/0379_userprofile_uuid.py b/zerver/migrations/0379_userprofile_uuid.pynew file mode 100644index 00000000000..31af5a54284--- /dev/null+++ b/zerver/migrations/0379_userprofile_uuid.py@@ -0,0 +1,18 @@+# Generated by Django 3.2.12 on 2022-03-05 14:40++from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ("zerver", "0378_alter_realmuserdefault_realm"),+ ]++ operations = [+ migrations.AddField(+ model_name="userprofile",+ name="uuid",+ field=models.UUIDField(null=True, unique=True),+ ),+ ]diff --git a/zerver/migrations/0380_userprofile_uuid_backfill.py b/zerver/migrations/0380_userprofile_uuid_backfill.pynew file mode 100644index 00000000000..a3d8df0e775--- /dev/null+++ b/zerver/migrations/0380_userprofile_uuid_backfill.py@@ -0,0 +1,40 @@+import uuid++from django.db import migrations, models+from django.db.backends.postgresql.schema import DatabaseSchemaEditor+from django.db.migrations.state import StateApps+++def backfill_user_profile_uuid(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:+ 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),+ ]diff --git a/zerver/models.py b/zerver/models.pyindex 12d0f13ef2c..20c50199100 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -17,6 +17,7 @@ from typing import (TypeVar,Union,)+from uuid import UUID, uuid4import django.contrib.authimport orjson@@ -1688,6 +1689,12 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings):tos_version: Optional[str] = models.CharField(null=True, max_length=10)api_key: str = models.CharField(max_length=API_KEY_LENGTH)+ # A UUID generated on user creation. Introduced primarily to+ # provide a unique key for a user for the mobile push+ # notifications bouncer that will not have collisions after doing+ # a data export and then import.+ uuid: Optional[UUID] = models.UUIDField(null=True, default=uuid4, unique=True)+# Whether the user has access to server-level administrator pages, like /activityis_staff: bool = models.BooleanField(default=False)