Skip to content

Commit

Permalink
[#310] Update migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmursa-dev committed Jan 10, 2025
1 parent c26d587 commit 8e03c61
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,44 @@
logger = logging.getLogger(__name__)


def _check_nummeraanduiding_length(apps, schema_editor):

Organisatie = apps.get_model("contactgegevens", "Organisatie")
Persoon = apps.get_model("contactgegevens", "Persoon")

organisatie_queryset = Organisatie.objects.annotate(
length=Length("adres_nummeraanduiding_id")
).filter(length__gt=16)

logger.info("--- START Organisatie INFO ---")
def _log_records(records, model_name):
logger.info("--- START %s INFO ---", model_name)
logger.info(
"Found %s records for Organisatie that have adres_nummeraanduiding_id length > 16 chars",
organisatie_queryset.count(),
"Found %s records for %s that have adres_nummeraanduiding_id length > 16 chars",
model_name,
records.count(),
)

for record in organisatie_queryset:
for record in records:
logger.warning(
json.dumps(
{
"model_name": model_name,
"pk": record.pk,
"uuid": str(record.uuid),
"adres_nummeraanduiding_id": record.adres_nummeraanduiding_id,
}
)
)

logger.info("--- END Organisatie INFO ---")
logger.info("--- END %s INFO ---", model_name)

persoon_queryset = Persoon.objects.annotate(

def _check_field_length(apps, schema_editor):

Organisatie = apps.get_model("contactgegevens", "Organisatie")
Persoon = apps.get_model("contactgegevens", "Persoon")

organisatie_queryset = Organisatie.objects.annotate(
length=Length("adres_nummeraanduiding_id")
).filter(length__gt=16)

logger.info("--- START Persoon INFO ---")
logger.info(
"Found %s records for Persoon that have adres_nummeraanduiding_id length > 16 chars",
persoon_queryset.count(),
)
persoon_queryset = Persoon.objects.annotate(
length=Length("adres_nummeraanduiding_id")
).filter(length__gt=16)

for record in persoon_queryset:
logger.warning(
json.dumps(
{
"pk": record.pk,
"uuid": str(record.uuid),
"adres_nummeraanduiding_id": record.adres_nummeraanduiding_id,
}
)
)
logger.info("--- END Persoon INFO ---")
_log_records(organisatie_queryset, "Organisatie")
_log_records(persoon_queryset, "Persoon")

if organisatie_queryset or persoon_queryset:
raise CommandError(
Expand All @@ -74,7 +63,7 @@ class Migration(migrations.Migration):

operations = [
migrations.RunPython(
code=_check_nummeraanduiding_length,
code=_check_field_length,
reverse_code=migrations.RunPython.noop,
),
migrations.AlterField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,20 @@
logger = logging.getLogger(__name__)


def _check_nummeraanduiding_length(apps, schema_editor):

Betrokkene = apps.get_model("klantinteracties", "Betrokkene")
Partij = apps.get_model("klantinteracties", "Partij")

betrokkene_queryset = Betrokkene.objects.annotate(
length1=Length("bezoekadres_nummeraanduiding_id"),
length2=Length("correspondentieadres_nummeraanduiding_id"),
).filter(models.Q(length1__gt=16) | models.Q(length2__gt=16))

logger.info("--- START Betrokkene INFO ---")
def _log_records(records, model_name):
logger.info("--- START %s INFO ---", model_name)
logger.info(
"Found %s records for Betrokkene that have bezoekadres_nummeraanduiding_id or correspondentieadres_nummeraanduiding_id length > 16 chars",
betrokkene_queryset.count(),
"Found %s records for %s that have bezoekadres_nummeraanduiding_id "
"or correspondentieadres_nummeraanduiding_id length > 16 chars",
model_name,
records.count(),
)

for record in betrokkene_queryset:
for record in records:
logger.warning(
json.dumps(
{
"model_name": model_name,
"pk": record.pk,
"uuid": str(record.uuid),
"bezoekadres_nummeraanduiding_id": record.bezoekadres_nummeraanduiding_id,
Expand All @@ -38,32 +32,26 @@ def _check_nummeraanduiding_length(apps, schema_editor):
)
)

logger.info("--- END Betrokkene INFO ---")
logger.info("--- END %s INFO ---", model_name)

partij_queryset = Partij.objects.annotate(

def _check_field_length(apps, schema_editor):

Betrokkene = apps.get_model("klantinteracties", "Betrokkene")
Partij = apps.get_model("klantinteracties", "Partij")

betrokkene_queryset = Betrokkene.objects.annotate(
length1=Length("bezoekadres_nummeraanduiding_id"),
length2=Length("correspondentieadres_nummeraanduiding_id"),
).filter(models.Q(length1__gt=16) | models.Q(length2__gt=16))

logger.info("--- START Partij INFO ---")
logger.info(
"Found %s records for Partij that have bezoekadres_nummeraanduiding_id or correspondentieadres_nummeraanduiding_id length > 16 chars",
partij_queryset.count(),
)

for record in partij_queryset:
logger.info(
json.dumps(
{
"pk": record.pk,
"uuid": str(record.uuid),
"bezoekadres_nummeraanduiding_id": record.bezoekadres_nummeraanduiding_id,
"correspondentieadres_nummeraanduiding_id": record.correspondentieadres_nummeraanduiding_id,
}
)
)
partij_queryset = Partij.objects.annotate(
length1=Length("bezoekadres_nummeraanduiding_id"),
length2=Length("correspondentieadres_nummeraanduiding_id"),
).filter(models.Q(length1__gt=16) | models.Q(length2__gt=16))

logger.warning("--- END Partij INFO ---")
_log_records(betrokkene_queryset, "Betrokkene")
_log_records(partij_queryset, "Partij")

if betrokkene_queryset or partij_queryset:
raise CommandError(
Expand All @@ -79,7 +67,7 @@ class Migration(migrations.Migration):

operations = [
migrations.RunPython(
code=_check_nummeraanduiding_length,
code=_check_field_length,
reverse_code=migrations.RunPython.noop,
),
migrations.AlterField(
Expand Down

0 comments on commit 8e03c61

Please sign in to comment.