From 7e707270f0dcc9cf62fef1afb12ff7efe4b2a24b Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sun, 11 Jun 2023 12:22:36 -0700 Subject: [PATCH] models: Convert deprecated index_together option to indexes. index_together is slated for removal in Django 5.1: https://docs.djangoproject.com/en/4.2/internals/deprecation/#deprecation-removed-in-5-1 We set the optional index names to match the previously generated index names to avoid adding new migrations. Signed-off-by: Anders Kaseorg --- .../migrations/0008_add_count_indexes.py | 29 ++++++++++++------- analytics/models.py | 21 ++++++++++++-- ..._add_userpresence_realm_timestamp_index.py | 9 ++++-- ...ter_useractivityinterval_index_together.py | 11 ++++--- .../0443_userpresence_new_table_schema.py | 24 ++++++++++++--- zerver/models.py | 19 ++++++++---- zilencer/migrations/0016_remote_counts.py | 9 ++++-- .../0017_installationcount_indexes.py | 20 ++++++++----- zilencer/models.py | 19 ++++++++---- 9 files changed, 117 insertions(+), 44 deletions(-) diff --git a/analytics/migrations/0008_add_count_indexes.py b/analytics/migrations/0008_add_count_indexes.py index d6792f1cca..e8085db319 100644 --- a/analytics/migrations/0008_add_count_indexes.py +++ b/analytics/migrations/0008_add_count_indexes.py @@ -1,5 +1,5 @@ # Generated by Django 1.10.5 on 2017-02-01 22:28 -from django.db import migrations +from django.db import migrations, models class Migration(migrations.Migration): @@ -9,16 +9,25 @@ class Migration(migrations.Migration): ] operations = [ - migrations.AlterIndexTogether( - name="realmcount", - index_together={("property", "end_time")}, + migrations.AddIndex( + model_name="realmcount", + index=models.Index( + fields=["property", "end_time"], + name="analytics_realmcount_property_end_time_3b60396b_idx", + ), ), - migrations.AlterIndexTogether( - name="streamcount", - index_together={("property", "realm", "end_time")}, + migrations.AddIndex( + model_name="streamcount", + index=models.Index( + fields=["property", "realm", "end_time"], + name="analytics_streamcount_property_realm_id_end_time_155ae930_idx", + ), ), - migrations.AlterIndexTogether( - name="usercount", - index_together={("property", "realm", "end_time")}, + migrations.AddIndex( + model_name="usercount", + index=models.Index( + fields=["property", "realm", "end_time"], + name="analytics_usercount_property_realm_id_end_time_591dbec1_idx", + ), ), ] diff --git a/analytics/models.py b/analytics/models.py index 46998c63bc..861c8c51f4 100644 --- a/analytics/models.py +++ b/analytics/models.py @@ -79,7 +79,12 @@ class RealmCount(BaseCount): name="unique_realm_count_null_subgroup", ), ] - index_together = ["property", "end_time"] + indexes = [ + models.Index( + fields=["property", "end_time"], + name="analytics_realmcount_property_end_time_3b60396b_idx", + ) + ] def __str__(self) -> str: return f"{self.realm!r} {self.property} {self.subgroup} {self.value}" @@ -105,7 +110,12 @@ class UserCount(BaseCount): ] # This index dramatically improves the performance of # aggregating from users to realms - index_together = ["property", "realm", "end_time"] + indexes = [ + models.Index( + fields=["property", "realm", "end_time"], + name="analytics_usercount_property_realm_id_end_time_591dbec1_idx", + ) + ] def __str__(self) -> str: return f"{self.user!r} {self.property} {self.subgroup} {self.value}" @@ -131,7 +141,12 @@ class StreamCount(BaseCount): ] # This index dramatically improves the performance of # aggregating from streams to realms - index_together = ["property", "realm", "end_time"] + indexes = [ + models.Index( + fields=["property", "realm", "end_time"], + name="analytics_streamcount_property_realm_id_end_time_155ae930_idx", + ) + ] def __str__(self) -> str: return f"{self.stream!r} {self.property} {self.subgroup} {self.value} {self.id}" diff --git a/zerver/migrations/0268_add_userpresence_realm_timestamp_index.py b/zerver/migrations/0268_add_userpresence_realm_timestamp_index.py index 1f6d0c29f9..34e72e630c 100644 --- a/zerver/migrations/0268_add_userpresence_realm_timestamp_index.py +++ b/zerver/migrations/0268_add_userpresence_realm_timestamp_index.py @@ -14,8 +14,11 @@ class Migration(migrations.Migration): name="realm", field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="zerver.Realm"), ), - migrations.AlterIndexTogether( - name="userpresence", - index_together={("realm", "timestamp")}, + migrations.AddIndex( + model_name="userpresence", + index=models.Index( + fields=["realm", "timestamp"], + name="zerver_userpresence_realm_id_timestamp_25f410da_idx", + ), ), ] diff --git a/zerver/migrations/0343_alter_useractivityinterval_index_together.py b/zerver/migrations/0343_alter_useractivityinterval_index_together.py index 2acc37ecc8..34e90bf472 100644 --- a/zerver/migrations/0343_alter_useractivityinterval_index_together.py +++ b/zerver/migrations/0343_alter_useractivityinterval_index_together.py @@ -1,6 +1,6 @@ # Generated by Django 3.2.6 on 2021-08-31 19:17 -from django.db import migrations +from django.db import migrations, models class Migration(migrations.Migration): @@ -9,8 +9,11 @@ class Migration(migrations.Migration): ] operations = [ - migrations.AlterIndexTogether( - name="useractivityinterval", - index_together={("user_profile", "end")}, + migrations.AddIndex( + model_name="useractivityinterval", + index=models.Index( + fields=["user_profile", "end"], + name="zerver_useractivityinterval_user_profile_id_end_bb3bfc37_idx", + ), ), ] diff --git a/zerver/migrations/0443_userpresence_new_table_schema.py b/zerver/migrations/0443_userpresence_new_table_schema.py index 7e9e55a791..534e4153c9 100644 --- a/zerver/migrations/0443_userpresence_new_table_schema.py +++ b/zerver/migrations/0443_userpresence_new_table_schema.py @@ -127,7 +127,12 @@ class Migration(migrations.Migration): migrations.RenameModel( old_name="UserPresence", new_name="UserPresenceOld", - ) + ), + migrations.RenameIndex( + model_name="userpresenceold", + old_name="zerver_userpresence_realm_id_timestamp_25f410da_idx", + new_name="zerver_userpresenceold_realm_id_timestamp_52ef5fd3_idx", + ), ], ), migrations.CreateModel( @@ -164,8 +169,19 @@ class Migration(migrations.Migration): ), ), ], - options={ - "index_together": {("realm", "last_active_time"), ("realm", "last_connected_time")}, - }, + ), + migrations.AddIndex( + model_name="userpresence", + index=models.Index( + fields=["realm", "last_active_time"], + name="zerver_userpresence_realm_id_last_active_time_1c5aa9a2_idx", + ), + ), + migrations.AddIndex( + model_name="userpresence", + index=models.Index( + fields=["realm", "last_connected_time"], + name="zerver_userpresence_realm_id_last_connected_time_98d2fc9f_idx", + ), ), ] diff --git a/zerver/models.py b/zerver/models.py index b21d896348..a9c7c6ee02 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -4133,8 +4133,11 @@ class UserActivityInterval(models.Model): end = models.DateTimeField("end time", db_index=True) class Meta: - index_together = [ - ("user_profile", "end"), + indexes = [ + models.Index( + fields=["user_profile", "end"], + name="zerver_useractivityinterval_user_profile_id_end_bb3bfc37_idx", + ), ] @@ -4174,9 +4177,15 @@ class UserPresence(models.Model): LEGACY_STATUS_IDLE_INT = 2 class Meta: - index_together = [ - ("realm", "last_active_time"), - ("realm", "last_connected_time"), + indexes = [ + models.Index( + fields=["realm", "last_active_time"], + name="zerver_userpresence_realm_id_last_active_time_1c5aa9a2_idx", + ), + models.Index( + fields=["realm", "last_connected_time"], + name="zerver_userpresence_realm_id_last_connected_time_98d2fc9f_idx", + ), ] @staticmethod diff --git a/zilencer/migrations/0016_remote_counts.py b/zilencer/migrations/0016_remote_counts.py index a7399360f3..ea5b4a3a68 100644 --- a/zilencer/migrations/0016_remote_counts.py +++ b/zilencer/migrations/0016_remote_counts.py @@ -59,9 +59,12 @@ class Migration(migrations.Migration): name="remoterealmcount", unique_together={("server", "realm_id", "property", "subgroup", "end_time")}, ), - migrations.AlterIndexTogether( - name="remoterealmcount", - index_together={("property", "end_time")}, + migrations.AddIndex( + model_name="remoterealmcount", + index=models.Index( + fields=["property", "end_time"], + name="zilencer_remoterealmcount_property_end_time_506a0b38_idx", + ), ), migrations.AlterUniqueTogether( name="remoteinstallationcount", diff --git a/zilencer/migrations/0017_installationcount_indexes.py b/zilencer/migrations/0017_installationcount_indexes.py index 45f3344b4e..28fcce7466 100644 --- a/zilencer/migrations/0017_installationcount_indexes.py +++ b/zilencer/migrations/0017_installationcount_indexes.py @@ -1,6 +1,6 @@ # Generated by Django 1.11.20 on 2019-04-23 20:17 -from django.db import migrations +from django.db import migrations, models class Migration(migrations.Migration): @@ -9,12 +9,18 @@ class Migration(migrations.Migration): ] operations = [ - migrations.AlterIndexTogether( - name="remoteinstallationcount", - index_together={("server", "remote_id")}, + migrations.AddIndex( + model_name="remoteinstallationcount", + index=models.Index( + fields=["server", "remote_id"], + name="zilencer_remoteinstallat_server_id_remote_id_f72e4c30_idx", + ), ), - migrations.AlterIndexTogether( - name="remoterealmcount", - index_together={("property", "end_time"), ("server", "remote_id")}, + migrations.AddIndex( + model_name="remoterealmcount", + index=models.Index( + fields=["server", "remote_id"], + name="zilencer_remoterealmcount_server_id_remote_id_de1573d8_idx", + ), ), ] diff --git a/zilencer/models.py b/zilencer/models.py index 12a429385a..c646516429 100644 --- a/zilencer/models.py +++ b/zilencer/models.py @@ -115,8 +115,11 @@ class RemoteInstallationCount(BaseCount): class Meta: unique_together = ("server", "property", "subgroup", "end_time") - index_together = [ - ["server", "remote_id"], + indexes = [ + models.Index( + fields=["server", "remote_id"], + name="zilencer_remoteinstallat_server_id_remote_id_f72e4c30_idx", + ), ] def __str__(self) -> str: @@ -132,9 +135,15 @@ class RemoteRealmCount(BaseCount): class Meta: unique_together = ("server", "realm_id", "property", "subgroup", "end_time") - index_together = [ - ["property", "end_time"], - ["server", "remote_id"], + indexes = [ + models.Index( + fields=["property", "end_time"], + name="zilencer_remoterealmcount_property_end_time_506a0b38_idx", + ), + models.Index( + fields=["server", "remote_id"], + name="zilencer_remoterealmcount_server_id_remote_id_de1573d8_idx", + ), ] def __str__(self) -> str: