zilencer: Enforce uniqueness of server_id + remote_id.

This was previously just an index (not a unique one).  Enforce this
data constraint.
This commit is contained in:
Alex Vandiver 2023-11-22 18:10:38 +00:00 committed by Tim Abbott
parent f4cbb494ac
commit 150c64ddd0
2 changed files with 50 additions and 8 deletions

View File

@ -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",
),
]

View File

@ -207,11 +207,14 @@ class RemoteInstallationCount(BaseRemoteCount):
condition=Q(subgroup__isnull=True), condition=Q(subgroup__isnull=True),
name="unique_remote_installation_count_null_subgroup", name="unique_remote_installation_count_null_subgroup",
), ),
] UniqueConstraint(
indexes = [
models.Index(
fields=["server", "remote_id"], 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), condition=Q(subgroup__isnull=True),
name="unique_remote_realm_installation_count_null_subgroup", 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 = [ indexes = [
models.Index( models.Index(
fields=["property", "end_time"], fields=["property", "end_time"],
name="zilencer_remoterealmcount_property_end_time_506a0b38_idx", name="zilencer_remoterealmcount_property_end_time_506a0b38_idx",
), ),
models.Index(
fields=["server", "remote_id"],
name="zilencer_remoterealmcount_server_id_remote_id_de1573d8_idx",
),
] ]
@override @override