diff --git a/zilencer/migrations/0038_unique_server_remote_id.py b/zilencer/migrations/0038_unique_server_remote_id.py new file mode 100644 index 0000000000..0e2446996f --- /dev/null +++ b/zilencer/migrations/0038_unique_server_remote_id.py @@ -0,0 +1,34 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("zilencer", "0037_alter_remoteinstallationcount_unique_together_and_more"), + ] + + operations = [ + migrations.AddConstraint( + model_name="remoteinstallationcount", + constraint=models.UniqueConstraint( + condition=models.Q(("remote_id__isnull", False)), + fields=("server", "remote_id"), + name="unique_remote_installation_count_server_id_remote_id", + ), + ), + migrations.RemoveIndex( + model_name="remoteinstallationcount", + name="zilencer_remoteinstallat_server_id_remote_id_f72e4c30_idx", + ), + migrations.AddConstraint( + model_name="remoterealmcount", + constraint=models.UniqueConstraint( + condition=models.Q(("remote_id__isnull", False)), + fields=("server", "remote_id"), + name="unique_remote_realm_installation_count_server_id_remote_id", + ), + ), + migrations.RemoveIndex( + model_name="remoterealmcount", + name="zilencer_remoterealmcount_server_id_remote_id_de1573d8_idx", + ), + ] diff --git a/zilencer/models.py b/zilencer/models.py index 164082d1b4..bddefe4a7e 100644 --- a/zilencer/models.py +++ b/zilencer/models.py @@ -207,11 +207,14 @@ class RemoteInstallationCount(BaseRemoteCount): condition=Q(subgroup__isnull=True), name="unique_remote_installation_count_null_subgroup", ), - ] - indexes = [ - models.Index( + UniqueConstraint( fields=["server", "remote_id"], - name="zilencer_remoteinstallat_server_id_remote_id_f72e4c30_idx", + # As noted above, remote_id may be null, so we only + # enforce uniqueness if it isn't. This is not + # technically necessary, since null != null, but it + # makes the property more explicit. + condition=Q(remote_id__isnull=False), + name="unique_remote_installation_count_server_id_remote_id", ), ] @@ -243,16 +246,21 @@ class RemoteRealmCount(BaseRemoteCount): condition=Q(subgroup__isnull=True), name="unique_remote_realm_installation_count_null_subgroup", ), + UniqueConstraint( + fields=["server", "remote_id"], + # As with RemoteInstallationCount above, remote_id may + # be null; since null != null, this condition is not + # strictly necessary, but serves to make the property + # more explicit. + condition=Q(remote_id__isnull=False), + name="unique_remote_realm_installation_count_server_id_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", - ), ] @override