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.

migrations: Add case-insensitive unique index on realm and stream name.

Mateusz Mandera70bcc7a8e994
diff --git a/zerver/migrations/0302_case_insensitive_stream_name_index.py b/zerver/migrations/0302_case_insensitive_stream_name_index.py
new file mode 100644
index 00000000000..22da83715c4
--- /dev/null
+++ b/zerver/migrations/0302_case_insensitive_stream_name_index.py
@@ -0,0 +1,20 @@
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ('zerver', '0301_fix_unread_messages_in_deactivated_streams'),
+ ]
+
+ operations = [
+ # We do Stream lookups case-insensitively with respect to the name, but we were missing
+ # the appropriate (realm_id, upper(name::text)) unique index to enforce uniqueness
+ # on database level.
+ migrations.RunSQL("""
+ CREATE UNIQUE INDEX zerver_stream_realm_id_name_uniq ON zerver_stream (realm_id, upper(name::text));
+ """),
+ migrations.AlterUniqueTogether(
+ name='stream',
+ unique_together=set(),
+ ),
+ ]
diff --git a/zerver/models.py b/zerver/models.py
index 095277c2a0e..cd61db194bd 100644
--- a/zerver/models.py
+++ b/zerver/models.py
@@ -1546,9 +1546,6 @@ class Stream(models.Model):
def is_history_public_to_subscribers(self) -> bool:
return self.history_public_to_subscribers
- class Meta:
- unique_together = ("name", "realm")
-
# Stream fields included whenever a Stream object is provided to
# Zulip clients via the API. A few details worth noting:
# * "id" is represented as "stream_id" in most API interfaces.