Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
migrations: Add case-insensitive unique indexes on realm and email.
Mateusz Manderab9b146c80956
diff --git a/docs/overview/changelog.md b/docs/overview/changelog.mdindex c5bd9062d4f..bd332660975 100644--- a/docs/overview/changelog.md+++ b/docs/overview/changelog.md@@ -54,6 +54,24 @@ We expect this to become the 3.0 final release in mid July.- Logged in users will be logged out during this one-time upgrade totransition them to more secure session cookies.+- This release contains dozens of database migrations, but we don't+ anticipate any of them being particularly expensive compared to+ those in past major releases.+- Previous versions had a rare bug that made it possible to create two+ user accounts with the same email address, preventing either from+ logging in. A migration in this release adds a database constraint+ that will fix this bug. The new migration will fail if any such+ duplicate accounts already exist; you can check whether this will+ happen be running the following in a [management shell][manage-shell]:+ ```+ from django.db.models.functions import Lower+ UserProfile.objects.all().annotate(email_lower=Lower("delivery_email"))+ .values('realm_id', 'email_lower').annotate(Count('id')).filter(id__count__gte=2)+ ```+ If the command returns any accounts, you need to address the+ duplicate accounts before upgrading. Zulip Cloud only had two+ accounts affected by this bug, so we expect the vast majority of+ installations will have none.- This release switches Zulip to install Postgres 12 from the upstreampostgres repository by default, rather than using the defaultPostgres version included with the operating system. Existing Zulip@@ -62,9 +80,6 @@ We expect this to become the 3.0 final release in mid July.start requiring Postgres 12, though we do expect it to improveperformance. Installations that would like to upgrade can follow[our new postgres upgrade guide][postgres upgrade].-- This release contains dozens of database migrations, but we don't- anticipate any of them being particularly expensive compared to- those in past major releases.- The format of the `JWT_AUTH_KEYS` setting has changed to include an[algorithms](https://pyjwt.readthedocs.io/en/latest/algorithms.html)list: `{"subdomain": "key"}` becomes `{"subdomain": {"key": "key",@@ -77,6 +92,7 @@ We expect this to become the 3.0 final release in mid July.- The changelog now has a section that makes it easy to find theUpgrade notes for all releases one is upgrading across.+[manage-shell]: ../production/management-commands.html#manage-py-shell[postgres-upgrade]: ..production/upgrade-or-modify.html#upgrading-postgresql#### Full feature changelogdiff --git a/zerver/migrations/0295_case_insensitive_email_indexes.py b/zerver/migrations/0295_case_insensitive_email_indexes.pynew file mode 100644index 00000000000..8a66407b8d7--- /dev/null+++ b/zerver/migrations/0295_case_insensitive_email_indexes.py@@ -0,0 +1,31 @@+from django.db import migrations+++class Migration(migrations.Migration):+ dependencies = [+ ('zerver', '0294_remove_userprofile_pointer'),+ ]++ operations = [+ # Zulip has always had case-insensitive matching for email+ # addresses on UserProfile objects. But Django's+ # unique_together feature only supports case-sensitive+ # indexes. So we reply the old unique_together index with a+ # new case-insensitive index.+ #+ # Further, when we created the delivery_email field, we+ # neglected to create an unique index on (realm_id,+ # delivery_email), which meant race conditions or logic bugs+ # could allow duplicate user accounts being created in+ # organizations with EMAIL_ADDRESS_VISIBILITY_ADMINS. We+ # correct this by adding the appropriate unique index there as+ # well.+ migrations.RunSQL("""+ CREATE UNIQUE INDEX zerver_userprofile_realm_id_email_uniq ON zerver_userprofile (realm_id, upper(email::text));+ CREATE UNIQUE INDEX zerver_userprofile_realm_id_delivery_email_uniq ON zerver_userprofile (realm_id, upper(delivery_email::text));+ """),+ migrations.AlterUniqueTogether(+ name='userprofile',+ unique_together=set(),+ ),+ ]diff --git a/zerver/models.py b/zerver/models.pyindex 48cbd242318..24db7aabb61 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -1127,9 +1127,6 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):presence_enabled=bool,)- class Meta:- unique_together = (('realm', 'email'),)-@propertydef profile_data(self) -> ProfileData:values = CustomProfileFieldValue.objects.filter(user_profile=self)