# Generated by Django 3.2.20 on 2023-11-06 18:56

from django.apps.registry import Apps
from django.db import migrations, models


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()


class Migration(migrations.Migration):
    dependencies = [
        ("common", "0046_alter_school_country"),
    ]

    operations = [
        migrations.RemoveField(
            model_name="school",
            name="postcode",
        ),
        migrations.RunPython(code=unique_school_names),
        migrations.AlterField(
            model_name="school",
            name="name",
            field=models.CharField(max_length=200, unique=True),
        ),
    ]
