zilencer: Add a unique index on RemoteRealm counts with RemoteRealm objects.

This commit is contained in:
Alex Vandiver 2024-05-06 15:26:17 +00:00 committed by Tim Abbott
parent 27767252cf
commit 7607969f27
2 changed files with 96 additions and 1 deletions

View File

@ -0,0 +1,81 @@
# Generated by Django 5.0.5 on 2024-05-06 15:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("zilencer", "0059_remoterealmauditlog_add_synced_billing_event_type_index"),
]
operations = [
migrations.SeparateDatabaseAndState(
# Django does not provide a RenameConstraint operation.
# "Constraints" are created as indexes in PostgreSQL, so
# we rename the underlying indexes.
database_operations=[
migrations.RunSQL(
sql=(
"ALTER INDEX unique_remote_realm_installation_count "
"RENAME TO unique_server_realm_installation_count"
),
reverse_sql=(
"ALTER INDEX unique_server_realm_installation_count "
"RENAME TO unique_remote_realm_installation_count"
),
),
migrations.RunSQL(
sql=(
"ALTER INDEX unique_remote_realm_installation_count_null_subgroup "
"RENAME TO unique_server_realm_installation_count_null_subgroup"
),
reverse_sql=(
"ALTER INDEX unique_server_realm_installation_count_null_subgroup "
"RENAME TO unique_remote_realm_installation_count_null_subgroup"
),
),
],
state_operations=[
migrations.RemoveConstraint(
model_name="remoterealmcount",
name="unique_remote_realm_installation_count",
),
migrations.RemoveConstraint(
model_name="remoterealmcount",
name="unique_remote_realm_installation_count_null_subgroup",
),
migrations.AddConstraint(
model_name="remoterealmcount",
constraint=models.UniqueConstraint(
condition=models.Q(("subgroup__isnull", False)),
fields=("server", "realm_id", "property", "subgroup", "end_time"),
name="unique_server_realm_installation_count",
),
),
migrations.AddConstraint(
model_name="remoterealmcount",
constraint=models.UniqueConstraint(
condition=models.Q(("subgroup__isnull", True)),
fields=("server", "realm_id", "property", "end_time"),
name="unique_server_realm_installation_count_null_subgroup",
),
),
],
),
migrations.AddConstraint(
model_name="remoterealmcount",
constraint=models.UniqueConstraint(
condition=models.Q(("subgroup__isnull", False)),
fields=("remote_realm_id", "property", "subgroup", "end_time"),
name="unique_remote_realm_installation_count",
),
),
migrations.AddConstraint(
model_name="remoterealmcount",
constraint=models.UniqueConstraint(
condition=models.Q(("subgroup__isnull", True)),
fields=("remote_realm_id", "property", "end_time"),
name="unique_remote_realm_installation_count_null_subgroup",
),
),
]

View File

@ -416,13 +416,27 @@ class RemoteRealmCount(BaseRemoteCount):
class Meta:
constraints = [
# These two constraints come from the information as
# provided by the remote server, for rows they provide.
UniqueConstraint(
fields=["server", "realm_id", "property", "subgroup", "end_time"],
condition=Q(subgroup__isnull=False),
name="unique_server_realm_installation_count",
),
UniqueConstraint(
fields=["server", "realm_id", "property", "end_time"],
condition=Q(subgroup__isnull=True),
name="unique_server_realm_installation_count_null_subgroup",
),
# These two constraints come from our internal
# record-keeping, which has a RemoteRealm object.
UniqueConstraint(
fields=["remote_realm_id", "property", "subgroup", "end_time"],
condition=Q(subgroup__isnull=False),
name="unique_remote_realm_installation_count",
),
UniqueConstraint(
fields=["server", "realm_id", "property", "end_time"],
fields=["remote_realm_id", "property", "end_time"],
condition=Q(subgroup__isnull=True),
name="unique_remote_realm_installation_count_null_subgroup",
),