Skip to content

Commit

Permalink
[#311] Upgrade migration
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmursa-dev committed Jan 10, 2025
1 parent e442bb0 commit 29fba52
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/openklant/components/contactgegevens/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import uuid

from django.core.validators import MinLengthValidator, validate_integer
from django.core.validators import MinLengthValidator
from django.db import models
from django.utils.translation import gettext_lazy as _

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,77 @@
# Generated by Django 4.2.15 on 2025-01-09 15:19

import logging
import django.core.validators
import openklant.utils.validators
from django.db import migrations, models
import openklant.utils.validators


from openklant.utils.converters import nl_code_to_iso_code_country_converter

logger = logging.getLogger(__name__)


def _update_records(records, name):
logger.info("Start updating records for %s", name)
for record in records:
updated = False
if record.bezoekadres_land:
value = nl_code_to_iso_code_country_converter(record.bezoekadres_land)
if not value:
logger.warning(
"No match found for the nl_code: %s for object %s with pk: %s and uuid %s",
record.bezoekadres_land,
name,
record.pk,
record.uuid,
)
record.bezoekadres_land = value
updated = True
if record.correspondentieadres_land:
value = nl_code_to_iso_code_country_converter(
record.correspondentieadres_land
)
if not value:
logger.warning(
"No match found for the nl_code: %s for object %s with pk: %s and uuid %s",
record.bezoekadres_land,
name,
record.pk,
record.uuid,
)
record.correspondentieadres_land = value
updated = True
if updated:
logger.debug(
"Object %s pk: %s uuid %s updated",
name,
record.pk,
record.uuid,
)
record.save()
logger.info("Records update finished")


def _update_country_code(apps, schema_editor):
Betrokkene = apps.get_model("klantinteracties", "Betrokkene")
Partij = apps.get_model("klantinteracties", "Partij")

_update_records(Betrokkene.objects.all(), "Betrokkene")
_update_records(Partij.objects.all(), "Partij")


class Migration(migrations.Migration):

dependencies = [
("klantinteracties", "0023_alter_digitaaladres_omschrijving"),
]

operations = [
migrations.RunPython(
code=_update_country_code,
reverse_code=migrations.RunPython.noop,
),
migrations.AlterField(
model_name="betrokkene",
name="bezoekadres_land",
Expand Down
3 changes: 2 additions & 1 deletion src/openklant/components/klantinteracties/models/mixins.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.core.validators import MinLengthValidator, validate_integer
from django.core.validators import MinLengthValidator
from django.db import models
from django.utils.translation import gettext_lazy as _

from vng_api_common.descriptors import GegevensGroepType

from openklant.utils.validators import validate_country


Expand Down

0 comments on commit 29fba52

Please sign in to comment.