Skip to content

Commit

Permalink
Merge pull request #1235 from rapidpro/fix-index-together-unique-togh…
Browse files Browse the repository at this point in the history
…ether-use

Fix index_together unique_together use
  • Loading branch information
norkans7 authored Aug 28, 2024
2 parents 41ba9fe + fb331b2 commit 0b60ecc
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 5.0.8 on 2024-08-23 13:00

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("contacts", "0027_install_triggers"),
("orgs", "0033_rename_orgs_orgbac_org_id_607508_idx_orgs_orgbac_org_slug_idx_and_more"),
]

operations = [
migrations.RenameIndex(
model_name="reporterscounter",
new_name="contacts_rptrscntr_org_id_idx",
old_fields=("org", "type"),
),
migrations.AlterUniqueTogether(
name="contact",
unique_together=set(),
),
migrations.AddConstraint(
model_name="contact",
constraint=models.UniqueConstraint(
fields=("org", "uuid"), name="contacts_contact_org_id_563dcefdcba190b9_uniq"
),
),
]
6 changes: 4 additions & 2 deletions ureport/contacts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ def generate_counters(self):
return generated_counters

class Meta:
unique_together = ("org", "uuid")
constraints = [
models.UniqueConstraint(fields=["org", "uuid"], name="contacts_contact_org_id_563dcefdcba190b9_uniq")
]


class ReportersCounter(models.Model):
Expand Down Expand Up @@ -318,7 +320,7 @@ def get_counts(cls, org, types=None):
return {c["type"]: c["count_sum"] for c in counter_counts}

class Meta:
index_together = ("org", "type")
indexes = [
models.Index(name="contacts_rptrscntr_org_id_idx", fields=["org", "type"]),
models.Index(name="contacts_rptrscntr_org_typ_cnt", fields=["org", "type", "count"]),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 5.0.8 on 2024-08-23 13:00

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("flows", "0001_initial"),
("orgs", "0033_rename_orgs_orgbac_org_id_607508_idx_orgs_orgbac_org_slug_idx_and_more"),
]

operations = [
migrations.AlterUniqueTogether(
name="flowresult",
unique_together=set(),
),
migrations.AddConstraint(
model_name="flowresult",
constraint=models.UniqueConstraint(
fields=("org", "flow_uuid", "result_uuid"),
name="flows_flowresult_org_id_flow_uuid_result_uuid_5efa8f2d_uniq",
),
),
]
7 changes: 6 additions & 1 deletion ureport/flows/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ def update_or_create(cls, org, flow_uuid, result_uuid, result_name):
return obj

class Meta:
unique_together = ("org", "flow_uuid", "result_uuid")
constraints = [
models.UniqueConstraint(
fields=["org", "flow_uuid", "result_uuid"],
name="flows_flowresult_org_id_flow_uuid_result_uuid_5efa8f2d_uniq",
)
]


class FlowResultCategory(models.Model):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 5.0.8 on 2024-08-23 13:00

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("bots", "0006_alter_bot_created_by_alter_bot_modified_by"),
("landingpages", "0004_alter_landingpage_created_by_and_more"),
("orgs", "0033_rename_orgs_orgbac_org_id_607508_idx_orgs_orgbac_org_slug_idx_and_more"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AlterUniqueTogether(
name="landingpage",
unique_together=set(),
),
migrations.AlterIndexTogether(
name="landingpage",
index_together=set(),
),
migrations.AddConstraint(
model_name="landingpage",
constraint=models.UniqueConstraint(
fields=("org", "slug"), name="landingpages_landingpage_org_id_slug_f7f1304e_uniq"
),
),
]
5 changes: 3 additions & 2 deletions ureport/landingpages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ class LandingPage(SmartModel):
bots = models.ManyToManyField(Bot, blank=True)

class Meta:
index_together = (("org", "slug"),)
unique_together = ("org", "slug")
constraints = [
models.UniqueConstraint(fields=["org", "slug"], name="landingpages_landingpage_org_id_slug_f7f1304e_uniq")
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 5.0.8 on 2024-08-23 13:00

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("locations", "0007_alter_boundary_index_together"),
("orgs", "0033_rename_orgs_orgbac_org_id_607508_idx_orgs_orgbac_org_slug_idx_and_more"),
]

operations = [
migrations.RenameIndex(
model_name="boundary",
new_name="locations_bd_org_lv_nm_osm_idx",
old_fields=("org", "level", "name", "osm_id"),
),
migrations.AlterUniqueTogether(
name="boundary",
unique_together=set(),
),
migrations.AddConstraint(
model_name="boundary",
constraint=models.UniqueConstraint(
fields=("org", "osm_id"), name="locations_boundary_org_id_5c2d99160b82334a_uniq"
),
),
]
8 changes: 6 additions & 2 deletions ureport/locations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ class Boundary(models.Model):
)

class Meta:
unique_together = ("org", "osm_id")
index_together = ("org", "level", "name", "osm_id")
constraints = [
models.UniqueConstraint(fields=["org", "osm_id"], name="locations_boundary_org_id_5c2d99160b82334a_uniq")
]
indexes = [
models.Index(fields=["org", "level", "name", "osm_id"], name="locations_bd_org_lv_nm_osm_idx"),
]

@classmethod
def lock(cls, org, osm_id):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Generated by Django 5.0.8 on 2024-08-23 13:00

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("flows", "0002_alter_flowresult_unique_together_and_more"),
("orgs", "0033_rename_orgs_orgbac_org_id_607508_idx_orgs_orgbac_org_slug_idx_and_more"),
("polls", "0076_alter_poll_index_together"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.RenameIndex(
model_name="poll",
new_name="polls_poll_org_pblshd_id_idx",
old_fields=("org", "published", "id"),
),
migrations.RenameIndex(
model_name="pollquestion",
new_name="polls_qstn_poll_actv_fl_rs_idx",
old_fields=("poll", "is_active", "flow_result"),
),
migrations.RenameIndex(
model_name="pollresult",
new_name="polls_rslt_org_flw_rst_txt_idx",
old_fields=("org", "flow", "ruleset", "text"),
),
migrations.RenameIndex(
model_name="pollresult",
new_name="polls_pollresult_org_flow_idx",
old_fields=("org", "flow"),
),
migrations.AlterUniqueTogether(
name="pollcategory",
unique_together=set(),
),
migrations.AlterUniqueTogether(
name="pollquestion",
unique_together=set(),
),
migrations.AlterUniqueTogether(
name="pollresponsecategory",
unique_together=set(),
),
migrations.AddConstraint(
model_name="pollcategory",
constraint=models.UniqueConstraint(
fields=("name", "org"), name="polls_pollcategory_name_156693e034f96627_uniq"
),
),
migrations.AddConstraint(
model_name="pollquestion",
constraint=models.UniqueConstraint(
fields=("poll", "flow_result"), name="polls_pollquestion_poll_id_flow_result_id_608a2446_uniq"
),
),
migrations.AddConstraint(
model_name="pollquestion",
constraint=models.UniqueConstraint(
fields=("poll", "ruleset_uuid"), name="polls_pollquestion_poll_id_4202706c8106f06_uniq"
),
),
migrations.AddConstraint(
model_name="pollresponsecategory",
constraint=models.UniqueConstraint(
fields=("question", "rule_uuid"), name="polls_pollresponsecategory_question_id_3a161715511bd77d_uniq"
),
),
migrations.AddConstraint(
model_name="pollresponsecategory",
constraint=models.UniqueConstraint(
fields=("question", "flow_result_category"),
name="polls_pollresponsecatego_question_id_flow_result__4db1cb7e_uniq",
),
),
]
36 changes: 30 additions & 6 deletions ureport/polls/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def __str__(self):
return self.name

class Meta:
unique_together = ("name", "org")
constraints = [
models.UniqueConstraint(fields=["name", "org"], name="polls_pollcategory_name_156693e034f96627_uniq")
]
verbose_name_plural = _("Poll Categories")


Expand Down Expand Up @@ -735,7 +737,9 @@ def __str__(self):
return self.title

class Meta:
index_together = ("org", "published", "id")
indexes = [
models.Index(fields=["org", "published", "id"], name="polls_poll_org_pblshd_id_idx"),
]


@six.python_2_unicode_compatible
Expand Down Expand Up @@ -1292,8 +1296,17 @@ def __str__(self):
return self.title

class Meta:
unique_together = (("poll", "ruleset_uuid"), ("poll", "flow_result"))
index_together = ("poll", "is_active", "flow_result")
indexes = [
models.Index(fields=["poll", "is_active", "flow_result"], name="polls_qstn_poll_actv_fl_rs_idx"),
]
constraints = [
models.UniqueConstraint(
fields=["poll", "flow_result"], name="polls_pollquestion_poll_id_flow_result_id_608a2446_uniq"
),
models.UniqueConstraint(
fields=["poll", "ruleset_uuid"], name="polls_pollquestion_poll_id_4202706c8106f06_uniq"
),
]


class PollResponseCategory(models.Model):
Expand Down Expand Up @@ -1338,7 +1351,15 @@ def update_or_create(cls, question, rule_uuid, category):
return existing

class Meta:
unique_together = (("question", "rule_uuid"), ("question", "flow_result_category"))
constraints = [
models.UniqueConstraint(
fields=["question", "rule_uuid"], name="polls_pollresponsecategory_question_id_3a161715511bd77d_uniq"
),
models.UniqueConstraint(
fields=["question", "flow_result_category"],
name="polls_pollresponsecatego_question_id_flow_result__4db1cb7e_uniq",
),
]


class PollResult(models.Model):
Expand Down Expand Up @@ -1434,4 +1455,7 @@ def generate_poll_stats(self):
return generated_stats

class Meta:
index_together = [["org", "flow"], ["org", "flow", "ruleset", "text"]]
indexes = [
models.Index(fields=["org", "flow"], name="polls_pollresult_org_flow_idx"),
models.Index(fields=["org", "flow", "ruleset", "text"], name="polls_rslt_org_flw_rst_txt_idx"),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 5.0.8 on 2024-08-23 13:00

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("orgs", "0033_rename_orgs_orgbac_org_id_607508_idx_orgs_orgbac_org_slug_idx_and_more"),
("stats", "0028_activities_counter_triggers"),
]

operations = [
migrations.RenameIndex(
model_name="contactactivity",
new_name="stats_ct_actvt_org_contact_idx",
old_fields=("org", "contact"),
),
migrations.RenameIndex(
model_name="contactactivity",
new_name="stats_ct_actvt_org_date_idx",
old_fields=("org", "date"),
),
migrations.AlterUniqueTogether(
name="contactactivity",
unique_together=set(),
),
migrations.AddConstraint(
model_name="contactactivity",
constraint=models.UniqueConstraint(
fields=("org", "contact", "date"), name="stats_contactactivity_org_id_contact_date_348227aa_uniq"
),
),
]
11 changes: 9 additions & 2 deletions ureport/stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,15 @@ class ContactActivity(models.Model):
used = models.BooleanField(null=True)

class Meta:
index_together = (("org", "contact"), ("org", "date"))
unique_together = ("org", "contact", "date")
constraints = [
models.UniqueConstraint(
fields=["org", "contact", "date"], name="stats_contactactivity_org_id_contact_date_348227aa_uniq"
)
]
indexes = [
models.Index(fields=["org", "contact"], name="stats_ct_actvt_org_contact_idx"),
models.Index(fields=["org", "date"], name="stats_ct_actvt_org_date_idx"),
]

def generate_counters(self):
generated_counters = dict()
Expand Down

0 comments on commit 0b60ecc

Please sign in to comment.