Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

models: Manage indexes from migration 0279 with Django.

Anders Kaseorg0799acf3b691
diff --git a/zerver/migrations/0279_message_recipient_subject_indexes.py b/zerver/migrations/0279_message_recipient_subject_indexes.py
index a672126d184..6c4b29855fc 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 dd29f74fb6e..5d07c27d3a0 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]