From 0799acf3b691165ead0c10912900f336db1b39a6 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 22 Feb 2022 21:21:17 -0800 Subject: [PATCH] models: Manage indexes from migration 0279 with Django. Signed-off-by: Anders Kaseorg --- .../0279_message_recipient_subject_indexes.py | 29 +++++++++++++------ zerver/models.py | 19 +++++++++++- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/zerver/migrations/0279_message_recipient_subject_indexes.py b/zerver/migrations/0279_message_recipient_subject_indexes.py index a672126d18..6c4b29855f 100644 --- a/zerver/migrations/0279_message_recipient_subject_indexes.py +++ b/zerver/migrations/0279_message_recipient_subject_indexes.py @@ -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", + ), ), ] diff --git a/zerver/models.py b/zerver/models.py index dd29f74fb6..5d07c27d3a 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -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]