2019-03-04 17:50:49 +01:00
|
|
|
# Generated by Django 1.11.18 on 2019-02-25 12:42
|
|
|
|
|
|
|
|
from django.db import migrations
|
2022-05-27 23:33:51 +02:00
|
|
|
from django.db.backends.postgresql.schema import BaseDatabaseSchemaEditor
|
2019-03-04 17:50:49 +01:00
|
|
|
from django.db.migrations.state import StateApps
|
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2022-05-27 23:33:51 +02:00
|
|
|
def backfill_first_message_id(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
Stream = apps.get_model("zerver", "Stream")
|
|
|
|
Message = apps.get_model("zerver", "Message")
|
2019-03-04 17:50:49 +01:00
|
|
|
for stream in Stream.objects.all():
|
|
|
|
first_message = Message.objects.filter(
|
2021-02-12 08:19:30 +01:00
|
|
|
recipient__type_id=stream.id, recipient__type=2
|
|
|
|
).first()
|
2019-03-04 17:50:49 +01:00
|
|
|
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()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-04 17:50:49 +01:00
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
2021-02-12 08:20:45 +01:00
|
|
|
("zerver", "0209_stream_first_message_id"),
|
2019-03-04 17:50:49 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
2021-02-12 08:19:30 +01:00
|
|
|
migrations.RunPython(
|
|
|
|
backfill_first_message_id, reverse_code=migrations.RunPython.noop, elidable=True
|
|
|
|
),
|
2019-03-04 17:50:49 +01:00
|
|
|
]
|