Files
zulip/zerver/migrations/0210_stream_first_message_id.py
bedo c0a9ca8e9a tests: Pass update_fields to all stream.save().
A prep PR to 34308.

Explicitly pass the fields to be updated,
This increases performance but most importantly
prevents overwriting the db-saved value of
"subscriber_count" field (added in an upcoming PR)
with the in-memory default value of 0,
since "subscriber_count" will only be updted
via the db.

Migrate some tests to use do_ functions instead of
direclty modifying the state.
2025-04-15 10:28:18 -07:00

33 lines
1.1 KiB
Python

# Generated by Django 1.11.18 on 2019-02-25 12:42
from django.db import migrations
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps
def backfill_first_message_id(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
Stream = apps.get_model("zerver", "Stream")
Message = apps.get_model("zerver", "Message")
for stream in Stream.objects.all():
first_message = Message.objects.filter(
recipient__type_id=stream.id, recipient__type=2
).first()
if first_message is None:
# No need to change anything if the outcome is the default of None
continue
stream.first_message_id = first_message.id
stream.save(update_fields=["first_message_id"])
class Migration(migrations.Migration):
dependencies = [
("zerver", "0209_stream_first_message_id"),
]
operations = [
migrations.RunPython(
backfill_first_message_id, reverse_code=migrations.RunPython.noop, elidable=True
),
]