From 8bb30aa769c970fec02d554cdc0a172e26cbecc0 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Wed, 21 Aug 2024 11:05:11 +0200 Subject: [PATCH] Change unique_together to use constraints, replace use of index_together --- code_check.py | 2 +- ...alter_category_unique_together_and_more.py | 24 +++++++++++++ dash/categories/models.py | 2 +- ...idx_dashblock_org_typ_prio_idx_and_more.py | 26 ++++++++++++++ dash/dashblocks/models.py | 6 ++-- ...8_idx_orgs_orgbac_org_slug_idx_and_more.py | 36 +++++++++++++++++++ dash/orgs/models.py | 6 ++-- ...0003_alter_tag_unique_together_and_more.py | 24 +++++++++++++ dash/tags/models.py | 2 +- pyproject.toml | 6 ++-- 10 files changed, 124 insertions(+), 10 deletions(-) create mode 100644 dash/categories/migrations/0011_alter_category_unique_together_and_more.py create mode 100644 dash/dashblocks/migrations/0013_rename_dashblocks__org_id_024805_idx_dashblock_org_typ_prio_idx_and_more.py create mode 100644 dash/orgs/migrations/0033_rename_orgs_orgbac_org_id_607508_idx_orgs_orgbac_org_slug_idx_and_more.py create mode 100644 dash/tags/migrations/0003_alter_tag_unique_together_and_more.py diff --git a/code_check.py b/code_check.py index 7822dea..56fb571 100755 --- a/code_check.py +++ b/code_check.py @@ -39,7 +39,7 @@ def status(line): cmd("black dash test_runner") status("Running ruff") - cmd("ruff dash") + cmd("ruff check dash") status("Running isort") cmd("isort dash") diff --git a/dash/categories/migrations/0011_alter_category_unique_together_and_more.py b/dash/categories/migrations/0011_alter_category_unique_together_and_more.py new file mode 100644 index 0000000..302439d --- /dev/null +++ b/dash/categories/migrations/0011_alter_category_unique_together_and_more.py @@ -0,0 +1,24 @@ +# Generated by Django 5.0.8 on 2024-08-21 16:03 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("categories", "0010_alter_category_created_by_alter_category_modified_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="category", + unique_together=set(), + ), + migrations.AddConstraint( + model_name="category", + constraint=models.UniqueConstraint(fields=("name", "org"), name="categories_category_name_unique"), + ), + ] diff --git a/dash/categories/models.py b/dash/categories/models.py index 875aec0..2f079f9 100644 --- a/dash/categories/models.py +++ b/dash/categories/models.py @@ -50,7 +50,7 @@ def __str__(self): class Meta: ordering = ["name"] - unique_together = ("name", "org") + constraints = [models.UniqueConstraint(fields=["name", "org"], name="categories_category_name_unique")] verbose_name_plural = _("Categories") diff --git a/dash/dashblocks/migrations/0013_rename_dashblocks__org_id_024805_idx_dashblock_org_typ_prio_idx_and_more.py b/dash/dashblocks/migrations/0013_rename_dashblocks__org_id_024805_idx_dashblock_org_typ_prio_idx_and_more.py new file mode 100644 index 0000000..3f1e55f --- /dev/null +++ b/dash/dashblocks/migrations/0013_rename_dashblocks__org_id_024805_idx_dashblock_org_typ_prio_idx_and_more.py @@ -0,0 +1,26 @@ +# Generated by Django 5.0.8 on 2024-08-21 16:03 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ( + "dashblocks", + "0012_rename_dashblock_org_is_active_dashblock_type_priority_dashblocks__org_id_024805_idx_and_more", + ), + ] + + operations = [ + migrations.RenameIndex( + model_name="dashblock", + new_name="dashblock_org_typ_prio_idx", + old_name="dashblocks__org_id_024805_idx", + ), + migrations.RenameIndex( + model_name="dashblocktype", + new_name="dashblocktype_slug_name_idx", + old_name="dashblocks__slug_c0c6c6_idx", + ), + ] diff --git a/dash/dashblocks/models.py b/dash/dashblocks/models.py index 101e99e..8da9df9 100644 --- a/dash/dashblocks/models.py +++ b/dash/dashblocks/models.py @@ -49,7 +49,7 @@ def __str__(self): class Meta: ordering = ["name"] - indexes = [models.Index(fields=["slug", "name"])] + indexes = [models.Index(fields=["slug", "name"], name="dashblocktype_slug_name_idx")] class DashBlock(SmartModel): @@ -149,7 +149,9 @@ def __str__(self): class Meta: ordering = ["dashblock_type", "title"] - indexes = [models.Index(fields=["org", "is_active", "dashblock_type", "priority"])] + indexes = [ + models.Index(fields=["org", "is_active", "dashblock_type", "priority"], name="dashblock_org_typ_prio_idx") + ] class DashBlockImage(SmartModel): diff --git a/dash/orgs/migrations/0033_rename_orgs_orgbac_org_id_607508_idx_orgs_orgbac_org_slug_idx_and_more.py b/dash/orgs/migrations/0033_rename_orgs_orgbac_org_id_607508_idx_orgs_orgbac_org_slug_idx_and_more.py new file mode 100644 index 0000000..909eb0e --- /dev/null +++ b/dash/orgs/migrations/0033_rename_orgs_orgbac_org_id_607508_idx_orgs_orgbac_org_slug_idx_and_more.py @@ -0,0 +1,36 @@ +# Generated by Django 5.0.8 on 2024-08-21 16:03 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("orgs", "0032_rename_orgbackend_org_is_active_slug_orgs_orgbac_org_id_607508_idx"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.RenameIndex( + model_name="orgbackend", + new_name="orgs_orgbac_org_slug_idx", + old_name="orgs_orgbac_org_id_607508_idx", + ), + migrations.AlterUniqueTogether( + name="orgbackend", + unique_together=set(), + ), + migrations.AlterUniqueTogether( + name="taskstate", + unique_together=set(), + ), + migrations.AddConstraint( + model_name="orgbackend", + constraint=models.UniqueConstraint(fields=("org", "slug"), name="orgs_orgbackend_org_slug_unique"), + ), + migrations.AddConstraint( + model_name="taskstate", + constraint=models.UniqueConstraint(fields=("org", "task_key"), name="orgs_taskstate_org_task_key_unique"), + ), + ] diff --git a/dash/orgs/models.py b/dash/orgs/models.py index 46b83b5..b48155c 100644 --- a/dash/orgs/models.py +++ b/dash/orgs/models.py @@ -397,7 +397,7 @@ def get_time_taken(self): return (until - self.started_on).total_seconds() class Meta: - unique_together = ("org", "task_key") + constraints = [models.UniqueConstraint(fields=["org", "task_key"], name="orgs_taskstate_org_task_key_unique")] class OrgBackend(SmartModel): @@ -415,5 +415,5 @@ def __str__(self): return self.slug class Meta: - unique_together = ("org", "slug") - indexes = [models.Index(fields=["org", "is_active", "slug"])] + constraints = [models.UniqueConstraint(fields=["org", "slug"], name="orgs_orgbackend_org_slug_unique")] + indexes = [models.Index(fields=["org", "is_active", "slug"], name="orgs_orgbac_org_slug_idx")] diff --git a/dash/tags/migrations/0003_alter_tag_unique_together_and_more.py b/dash/tags/migrations/0003_alter_tag_unique_together_and_more.py new file mode 100644 index 0000000..668f30e --- /dev/null +++ b/dash/tags/migrations/0003_alter_tag_unique_together_and_more.py @@ -0,0 +1,24 @@ +# Generated by Django 5.0.8 on 2024-08-21 16:03 + +from django.conf import settings +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"), + ("tags", "0002_alter_tag_created_by_alter_tag_modified_by"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.AlterUniqueTogether( + name="tag", + unique_together=set(), + ), + migrations.AddConstraint( + model_name="tag", + constraint=models.UniqueConstraint(fields=("name", "org"), name="tags_tag_name_org_unique"), + ), + ] diff --git a/dash/tags/models.py b/dash/tags/models.py index 9de0a30..cfab767 100644 --- a/dash/tags/models.py +++ b/dash/tags/models.py @@ -15,4 +15,4 @@ def __str__(self) -> str: return self.name class Meta: - unique_together = ("name", "org") + constraints = [models.UniqueConstraint(fields=["name", "org"], name="tags_tag_name_org_unique")] diff --git a/pyproject.toml b/pyproject.toml index a45b1e3..abf4580 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -49,11 +49,13 @@ line-length = 119 [tool.ruff] line-length = 120 -select = ["E", "F", "W"] -ignore = ["E501", "F405"] fix = true exclude = ["./.tox/*", "./.venv/*", "./env/*", "*/migrations/*", "./build/*"] +[tool.ruff.lint] +select = ["E", "F", "W"] +ignore = ["E501", "F405"] + [tool.isort] multi_line_output = 3 force_grid_wrap = 0