Files
zulip/zerver/migrations/0210_stream_first_message_id.py
Challa Venkata Raghava Reddy b69aec2dbc streams: Add first_message_id tracking first message in stream.
This field is primarily intended to support avoiding displaying the
"more topics" feature in new organizations and streams, where we might
know that all messages in the stream are already available in the
browser.

Based on original work by Roman Godov, and significantly modified by
tabbott.

The second migration involved here could be expensive on Zulip Cloud,
but is unlikely to be an issue on other servers.
2019-03-11 13:30:49 -07:00

32 lines
1.1 KiB
Python

# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2019-02-25 12:42
from __future__ import unicode_literals
from django.db import migrations
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
def backfill_first_message_id(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> 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()
class Migration(migrations.Migration):
dependencies = [
('zerver', '0209_stream_first_message_id'),
]
operations = [
migrations.RunPython(backfill_first_message_id,
reverse_code=migrations.RunPython.noop),
]