models: Manage indexes from migration 0279 with Django.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2022-02-22 21:21:17 -08:00 committed by Tim Abbott
parent bbca3d048c
commit 0799acf3b6
2 changed files with 38 additions and 10 deletions

View File

@ -1,6 +1,9 @@
# Generated by Django 2.2.12 on 2020-04-30 00:35
from django.db import migrations
from django.contrib.postgres.operations import AddIndexConcurrently
from django.db import migrations, models
from django.db.models import F
from django.db.models.functions import Upper
class Migration(migrations.Migration):
@ -11,14 +14,22 @@ class Migration(migrations.Migration):
]
operations = [
migrations.RunSQL(
"""
CREATE INDEX CONCURRENTLY IF NOT EXISTS zerver_message_recipient_upper_subject ON zerver_message (recipient_id, upper(subject::text), id DESC NULLS LAST);
"""
AddIndexConcurrently(
model_name="message",
index=models.Index(
"recipient",
Upper("subject"),
F("id").desc(nulls_last=True),
name="zerver_message_recipient_upper_subject",
),
),
migrations.RunSQL(
"""
CREATE INDEX CONCURRENTLY IF NOT EXISTS zerver_message_recipient_subject ON zerver_message (recipient_id, subject, id DESC NULLS LAST);
"""
AddIndexConcurrently(
model_name="message",
index=models.Index(
"recipient",
"subject",
F("id").desc(nulls_last=True),
name="zerver_message_recipient_subject",
),
),
]

View File

@ -30,7 +30,8 @@ from django.core.exceptions import ValidationError
from django.core.validators import MinLengthValidator, RegexValidator, URLValidator, validate_email
from django.db import models, transaction
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.models import CASCADE, Manager, Q, Sum
from django.db.models import CASCADE, F, Manager, Q, Sum
from django.db.models.functions import Upper
from django.db.models.query import QuerySet
from django.db.models.signals import post_delete, post_save, pre_delete
from django.db.models.sql.compiler import SQLCompiler
@ -2798,6 +2799,22 @@ class Message(AbstractMessage):
return True
return False
class Meta:
indexes = [
models.Index(
"recipient",
Upper("subject"),
F("id").desc(nulls_last=True),
name="zerver_message_recipient_upper_subject",
),
models.Index(
"recipient",
"subject",
F("id").desc(nulls_last=True),
name="zerver_message_recipient_subject",
),
]
def get_context_for_message(message: Message) -> Sequence[Message]:
# TODO: Change return type to QuerySet[Message]