From bcb8214927ff9799f223657bbe150339e932b3dc Mon Sep 17 00:00:00 2001 From: Stefan Kairinos <118008817+SKairinos@users.noreply.github.com> Date: Thu, 16 Nov 2023 12:15:41 +0000 Subject: [PATCH] fix: migration 48 (#2232) --- .../migrations/0048_unique_school_names.py | 26 ++++++++++++----- .../tests/test_0048_unique_school_names.py | 29 +++++++++++++++++++ 2 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 cfl_common/common/tests/test_0048_unique_school_names.py diff --git a/cfl_common/common/migrations/0048_unique_school_names.py b/cfl_common/common/migrations/0048_unique_school_names.py index 8a5d234e5..4968f29b1 100644 --- a/cfl_common/common/migrations/0048_unique_school_names.py +++ b/cfl_common/common/migrations/0048_unique_school_names.py @@ -7,14 +7,24 @@ def unique_school_names(apps: Apps, *args): School = apps.get_model("common", "School") - schools = School.objects.values("name").annotate(name_count=models.Count("name")).filter(name_count__gt=1) - for school in schools: - schools = list(School.objects.filter(name=school["name"]).order_by("id")) - for index in range(school["name_count"]): - if index > 0: - school = schools[index] - school.name += f" {index + 1}" - school.save() + def get_school_name(school_name: str, school_name_number: int): + return f"{school_name} {school_name_number}" + + school_values = School.objects.values("name").annotate(name_count=models.Count("name")).filter(name_count__gt=1) + for school_value in school_values: + school_name = school_value["name"] + school_name_number = 1 + + schools = list(School.objects.filter(name=school_name).order_by("id")) + for index in range(1, len(schools)): + school = schools[index] + + school.name = get_school_name(school_name, school_name_number) + while School.objects.filter(name=school.name).exists(): + school_name_number += 1 + school.name = get_school_name(school_name, school_name_number) + + school.save() class Migration(migrations.Migration): diff --git a/cfl_common/common/tests/test_0048_unique_school_names.py b/cfl_common/common/tests/test_0048_unique_school_names.py new file mode 100644 index 000000000..1ad02f502 --- /dev/null +++ b/cfl_common/common/tests/test_0048_unique_school_names.py @@ -0,0 +1,29 @@ +import pytest +from django_test_migrations.migrator import Migrator + + +@pytest.mark.django_db +def test_0048_unique_school_names(migrator: Migrator): + state = migrator.apply_initial_migration(("common", "0047_delete_school_postcode")) + School = state.apps.get_model("common", "School") + + school_name = "ExampleSchool" + School.objects.bulk_create( + [ + School(name=school_name), + School(name=school_name), + School(name=f"{school_name} 1"), + ] + ) + school_ids = list(School.objects.order_by("-id")[:3].values_list("id", flat=True)) + school_ids.reverse() + + migrator.apply_tested_migration(("common", "0048_unique_school_names")) + School = state.apps.get_model("common", "School") + + def assert_school_name(index: int, name: str): + assert School.objects.get(id=school_ids[index]).name == name + + assert_school_name(0, school_name) + assert_school_name(1, f"{school_name} 2") + assert_school_name(2, f"{school_name} 1")