mirror of https://github.com/zulip/zulip.git
models: Add UserProfile.uuid field and backfill migrations.
This commit is contained in:
parent
0677c90170
commit
3e2ad84bbe
|
@ -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),
|
||||
),
|
||||
]
|
|
@ -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),
|
||||
]
|
|
@ -17,6 +17,7 @@ from typing import (
|
|||
TypeVar,
|
||||
Union,
|
||||
)
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
import django.contrib.auth
|
||||
import 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 /activity
|
||||
is_staff: bool = models.BooleanField(default=False)
|
||||
|
||||
|
|
Loading…
Reference in New Issue