mirror of
https://github.com/zulip/zulip.git
synced 2025-11-01 04:23:46 +00:00
migrations: Subscription.is_user_active denormalization - step two.
This adds a migration to do the backfill and add the index that'll be used to optimize the bulk_get_subscriber_user_ids query in the next commit.
This commit is contained in:
committed by
Tim Abbott
parent
53ed759fc1
commit
1e37fde59f
59
zerver/migrations/0313_finish_is_user_active_migration.py
Normal file
59
zerver/migrations/0313_finish_is_user_active_migration.py
Normal file
@@ -0,0 +1,59 @@
|
||||
# Generated by Django 3.1.5 on 2021-02-07 14:56
|
||||
|
||||
from django.contrib.postgres.operations import AddIndexConcurrently
|
||||
from django.db import connection, migrations, models
|
||||
from django.db.backends.postgresql.schema import DatabaseSchemaEditor
|
||||
from django.db.migrations.state import StateApps
|
||||
|
||||
|
||||
def backfill_is_user_active(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
|
||||
Subscription = apps.get_model("zerver", "Subscription")
|
||||
BATCH_SIZE = 1000
|
||||
lower_id_bound = 0
|
||||
|
||||
max_id = Subscription.objects.aggregate(models.Max("id"))["id__max"]
|
||||
if max_id is None:
|
||||
# No Subscription entries
|
||||
return
|
||||
while lower_id_bound <= max_id:
|
||||
print(f"Processed {lower_id_bound} / {max_id}")
|
||||
upper_id_bound = lower_id_bound + BATCH_SIZE
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute(
|
||||
"""
|
||||
UPDATE zerver_subscription
|
||||
SET is_user_active = zerver_userprofile.is_active
|
||||
FROM zerver_userprofile
|
||||
WHERE zerver_subscription.user_profile_id = zerver_userprofile.id
|
||||
AND zerver_subscription.id BETWEEN %(lower_id_bound)s AND %(upper_id_bound)s
|
||||
""",
|
||||
{"lower_id_bound": lower_id_bound, "upper_id_bound": upper_id_bound},
|
||||
)
|
||||
|
||||
lower_id_bound += BATCH_SIZE + 1
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
atomic = False
|
||||
|
||||
dependencies = [
|
||||
("zerver", "0312_subscription_is_user_active"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(backfill_is_user_active, reverse_code=migrations.RunPython.noop),
|
||||
# Make the field non-null now that we backfilled.
|
||||
migrations.AlterField(
|
||||
model_name="subscription",
|
||||
name="is_user_active",
|
||||
field=models.BooleanField(),
|
||||
),
|
||||
AddIndexConcurrently(
|
||||
model_name="subscription",
|
||||
index=models.Index(
|
||||
condition=models.Q(("active", True), ("is_user_active", True)),
|
||||
fields=["recipient", "user_profile"],
|
||||
name="zerver_subscription_recipient_id_user_profile_id_idx",
|
||||
),
|
||||
),
|
||||
]
|
||||
@@ -2570,7 +2570,7 @@ class Subscription(models.Model):
|
||||
# We intentionally don't specify a default value to promote thinking
|
||||
# about this explicitly, as in some special cases, such as data import,
|
||||
# we may be creating Subscription objects for a user that's deactivated.
|
||||
is_user_active: Optional[bool] = models.BooleanField(null=True)
|
||||
is_user_active: bool = models.BooleanField()
|
||||
|
||||
ROLE_STREAM_ADMINISTRATOR = 20
|
||||
ROLE_MEMBER = 50
|
||||
@@ -2600,6 +2600,13 @@ class Subscription(models.Model):
|
||||
|
||||
class Meta:
|
||||
unique_together = ("user_profile", "recipient")
|
||||
indexes = [
|
||||
models.Index(
|
||||
fields=("recipient", "user_profile"),
|
||||
name="zerver_subscription_recipient_id_user_profile_id_idx",
|
||||
condition=Q(active=True, is_user_active=True),
|
||||
),
|
||||
]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"<Subscription: {self.user_profile} -> {self.recipient}>"
|
||||
|
||||
Reference in New Issue
Block a user