Files
zulip/zerver/migrations/0429_user_topic_case_insensitive_unique_toghether.py
Abhijeet Prasad Bodas 80bf6b0777 user topic: Add case insensitive UNIQUE constraint.
This will allow us to rely on the database to detect duplicate
`UserTopic`s (with the same `topic_name` with different cases)
and thus correctly throw IntegrityErrors when expected.
This is also important from a correctness point of view, since as
of now, when checking if topic is muted or requesting the backend for
muting a topic, the frontend does not check for case insensitivity.

There might exist duplicate UserTopics (in a case insensitive sense)
which need are removed before creating the new index.
The migration was tested manually using `./manage.py shell`.
2023-02-20 21:04:13 -08:00

33 lines
1.0 KiB
Python

# Generated by Django 4.1.6 on 2023-02-11 05:16
import django.db.models.functions.text
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("zerver", "0428_remove_realm_email_address_visibility"),
]
operations = [
migrations.AlterUniqueTogether(
name="usertopic",
unique_together=set(),
),
# Before adding the constraint, remove any pre-existing duplicates from the table
migrations.RunSQL(
"""
DELETE FROM zerver_usertopic WHERE id NOT IN (SELECT max(id) FROM zerver_usertopic GROUP BY (user_profile_id, stream_id, upper(topic_name::text)));
"""
),
migrations.AddConstraint(
model_name="usertopic",
constraint=models.UniqueConstraint(
models.F("user_profile"),
models.F("stream"),
django.db.models.functions.text.Lower("topic_name"),
name="usertopic_case_insensitive_topic_uniq",
),
),
]