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.

messages: Add new conditional versions of subject indexes.

Alex Vandiver33e1d583bf1d
diff --git a/zerver/migrations/0693_add_conditional_indexes_for_topic.py b/zerver/migrations/0693_add_conditional_indexes_for_topic.py
new file mode 100644
index 00000000000..53ff12a5e68
--- /dev/null
+++ b/zerver/migrations/0693_add_conditional_indexes_for_topic.py
@@ -0,0 +1,61 @@
+import django.db.models.functions.text
+from django.contrib.postgres.operations import AddIndexConcurrently
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("zerver", "0692_alter_message_is_channel_message"),
+ ]
+
+ operations = [
+ migrations.RenameIndex(
+ model_name="message",
+ new_name="zerver_message_realm_upper_subject_all",
+ old_name="zerver_message_realm_upper_subject",
+ ),
+ migrations.RenameIndex(
+ model_name="message",
+ new_name="zerver_message_realm_recipient_upper_subject_all",
+ old_name="zerver_message_realm_recipient_upper_subject",
+ ),
+ migrations.RenameIndex(
+ model_name="message",
+ new_name="zerver_message_realm_recipient_subject_all",
+ old_name="zerver_message_realm_recipient_subject",
+ ),
+ AddIndexConcurrently(
+ model_name="message",
+ index=models.Index(
+ models.F("realm_id"),
+ django.db.models.functions.text.Upper("subject"),
+ models.OrderBy(models.F("id"), descending=True, nulls_last=True),
+ condition=models.Q(("is_channel_message", True)),
+ name="zerver_message_realm_upper_subject",
+ ),
+ ),
+ AddIndexConcurrently(
+ model_name="message",
+ index=models.Index(
+ models.F("realm_id"),
+ models.F("recipient_id"),
+ django.db.models.functions.text.Upper("subject"),
+ models.OrderBy(models.F("id"), descending=True, nulls_last=True),
+ condition=models.Q(("is_channel_message", True)),
+ name="zerver_message_realm_recipient_upper_subject",
+ ),
+ ),
+ AddIndexConcurrently(
+ model_name="message",
+ index=models.Index(
+ models.F("realm_id"),
+ models.F("recipient_id"),
+ models.F("subject"),
+ models.OrderBy(models.F("id"), descending=True, nulls_last=True),
+ condition=models.Q(("is_channel_message", True)),
+ name="zerver_message_realm_recipient_subject",
+ ),
+ ),
+ ]
diff --git a/zerver/models/messages.py b/zerver/models/messages.py
index a79bb7b7770..9def87c4741 100644
--- a/zerver/models/messages.py
+++ b/zerver/models/messages.py
@@ -205,6 +205,29 @@ class Message(AbstractMessage):
"date_sent",
name="zerver_message_realm_date_sent",
),
+ models.Index(
+ # To be removed shortly
+ "realm_id",
+ Upper("subject"),
+ F("id").desc(nulls_last=True),
+ name="zerver_message_realm_upper_subject_all",
+ ),
+ models.Index(
+ # To be removed shortly
+ "realm_id",
+ "recipient_id",
+ Upper("subject"),
+ F("id").desc(nulls_last=True),
+ name="zerver_message_realm_recipient_upper_subject_all",
+ ),
+ models.Index(
+ # To be removed shortly
+ "realm_id",
+ "recipient_id",
+ "subject",
+ F("id").desc(nulls_last=True),
+ name="zerver_message_realm_recipient_subject_all",
+ ),
models.Index(
# For users searching by topic (but not stream), which
# is done case-insensitively
@@ -212,6 +235,7 @@ class Message(AbstractMessage):
Upper("subject"),
F("id").desc(nulls_last=True),
name="zerver_message_realm_upper_subject",
+ condition=Q(is_channel_message=True),
),
models.Index(
# Most stream/topic searches are case-insensitive by
@@ -223,6 +247,7 @@ class Message(AbstractMessage):
Upper("subject"),
F("id").desc(nulls_last=True),
name="zerver_message_realm_recipient_upper_subject",
+ condition=Q(is_channel_message=True),
),
models.Index(
# Used by already_sent_mirrored_message_id, and when
@@ -233,6 +258,7 @@ class Message(AbstractMessage):
"subject",
F("id").desc(nulls_last=True),
name="zerver_message_realm_recipient_subject",
+ condition=Q(is_channel_message=True),
),
models.Index(
# Only used by update_first_visible_message_id