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: Remove unnecessary _like index on a hash string.

Alex Vandiverc472c7d5c84c
diff --git a/zerver/migrations/0793_alter_directmessagegroup_huddle_hash_and_more.py b/zerver/migrations/0793_alter_directmessagegroup_huddle_hash_and_more.py
new file mode 100644
index 00000000000..1134fbc4641
--- /dev/null
+++ b/zerver/migrations/0793_alter_directmessagegroup_huddle_hash_and_more.py
@@ -0,0 +1,51 @@
+from django.db import connection, migrations, models
+from django.db.backends.base.schema import BaseDatabaseSchemaEditor
+from django.db.migrations.state import StateApps
+from psycopg2.sql import SQL, Identifier
+
+
+def remove_non_unique_indexes(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
+ # We have to do this with inspection because the index name is not
+ # consistent across versions, and RemoveIndexConcurrently takes an
+ # index name.
+ with connection.cursor() as cursor:
+ constraints = connection.introspection.get_constraints(cursor, "zerver_huddle")
+ for index_name, infodict in constraints.items():
+ if infodict["columns"] != ["huddle_hash"]:
+ continue
+ if infodict["unique"]:
+ continue
+ raw_query = SQL("DROP INDEX CONCURRENTLY IF EXISTS {index_name}").format(
+ index_name=Identifier(index_name)
+ )
+ cursor.execute(raw_query)
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("zerver", "0792_fix_animated_emoji_still_images"),
+ ]
+
+ operations = [
+ # Prevent the naive plan of dropping and re-creating the unique index
+ migrations.SeparateDatabaseAndState(
+ database_operations=[
+ migrations.RunPython(remove_non_unique_indexes),
+ ],
+ state_operations=[
+ migrations.AlterField(
+ model_name="directmessagegroup",
+ name="huddle_hash",
+ field=models.CharField(max_length=40),
+ ),
+ migrations.AddConstraint(
+ model_name="directmessagegroup",
+ constraint=models.UniqueConstraint(
+ fields=("huddle_hash",), name="zerver_huddle_huddle_hash_key"
+ ),
+ ),
+ ],
+ ),
+ ]
diff --git a/zerver/models/recipients.py b/zerver/models/recipients.py
index ae237c2a359..849d9ad08f2 100644
--- a/zerver/models/recipients.py
+++ b/zerver/models/recipients.py
@@ -123,7 +123,9 @@ class DirectMessageGroup(models.Model):
corresponding DirectMessageGroup object.
"""
- huddle_hash = models.CharField(max_length=40, db_index=True, unique=True)
+ # We omit db_index and unique, as that also results in a useless
+ # varchar_pattern_ops index; see Meta, below.
+ huddle_hash = models.CharField(max_length=40)
# Foreign key to the Recipient object for this DirectMessageGroup.
recipient = models.ForeignKey(Recipient, null=True, on_delete=models.SET_NULL)
@@ -134,6 +136,9 @@ class DirectMessageGroup(models.Model):
# it needs to be renamed to "zerver_directmessagegroup".
class Meta:
db_table = "zerver_huddle"
+ constraints = [
+ models.UniqueConstraint(fields=["huddle_hash"], name="zerver_huddle_huddle_hash_key")
+ ]
def get_direct_message_group_hash(id_list: list[int]) -> str: