From 07cb7ebc6e9522674e6362bfb66839e46ed9adaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Schl=C3=B6gl?= Date: Fri, 6 Dec 2024 13:34:34 +0100 Subject: [PATCH] chore: migrate data to new alternative_names field uses three migrations to copy data from one field to the other, rename the field and delete the old one. --- apis_ontology/filtersets.py | 7 +++- .../migrations/0045_auto_20241206_1028.py | 42 +++++++++++++++++++ ...emove_person_alternative_names_and_more.py | 21 ++++++++++ ...s_new_person_alternative_names_and_more.py | 23 ++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 apis_ontology/migrations/0045_auto_20241206_1028.py create mode 100644 apis_ontology/migrations/0046_remove_person_alternative_names_and_more.py create mode 100644 apis_ontology/migrations/0047_rename_alternative_names_new_person_alternative_names_and_more.py diff --git a/apis_ontology/filtersets.py b/apis_ontology/filtersets.py index d00868e..c039b2c 100644 --- a/apis_ontology/filtersets.py +++ b/apis_ontology/filtersets.py @@ -5,7 +5,10 @@ from django.db import models import unicodedata -from apis_core.apis_entities.filtersets import AbstractEntityFilterSet +from apis_core.apis_entities.filtersets import ( + AbstractEntityFilterSet, + ABSTRACT_ENTITY_FILTERS_EXCLUDE, +) from apis_core.collections.models import SkosCollection, SkosCollectionContentObject from django.contrib.contenttypes.models import ContentType @@ -19,6 +22,8 @@ # helpers ######### +ABSTRACT_ENTITY_FILTERS_EXCLUDE += ["alternative_names"] + def remove_quotes(token): return token.strip('"') diff --git a/apis_ontology/migrations/0045_auto_20241206_1028.py b/apis_ontology/migrations/0045_auto_20241206_1028.py new file mode 100644 index 0000000..9e99d70 --- /dev/null +++ b/apis_ontology/migrations/0045_auto_20241206_1028.py @@ -0,0 +1,42 @@ +# Generated by Django 5.1.4 on 2024-12-06 10:28 + +from django.db import migrations +import json + + +def convert_alternative_names(apps, schema_editor): + """Convert alternative_names string field to JSON field alternative_names_new""" + Person = apps.get_model("apis_ontology", "Person") + + for person in Person.objects.all(): + if person.alternative_names: + # Split on newlines and filter out empty strings + names = [ + {"art": "", "end": "", "name": name.strip(), "start": ""} + for name in person.alternative_names.split("\n") + if name.strip() + ] + # Convert to JSON format + person.alternative_names_new = names + person.save() + + +def reverse_convert(apps, schema_editor): + """Reverse operation: convert JSON back to newline-separated string""" + Person = apps.get_model("apis_ontology", "Person") + + for person in Person.objects.all(): + if person.alternative_names_new: + names = json.loads(person.alternative_names_new) + person.alternative_names = "\n".join(names) + person.save() + + +class Migration(migrations.Migration): + dependencies = [ + ("apis_ontology", "0044_person_alternative_names_new_and_more"), + ] + + operations = [ + migrations.RunPython(convert_alternative_names, reverse_code=reverse_convert), + ] diff --git a/apis_ontology/migrations/0046_remove_person_alternative_names_and_more.py b/apis_ontology/migrations/0046_remove_person_alternative_names_and_more.py new file mode 100644 index 0000000..53a72fe --- /dev/null +++ b/apis_ontology/migrations/0046_remove_person_alternative_names_and_more.py @@ -0,0 +1,21 @@ +# Generated by Django 5.1.4 on 2024-12-06 12:30 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('apis_ontology', '0045_auto_20241206_1028'), + ] + + operations = [ + migrations.RemoveField( + model_name='person', + name='alternative_names', + ), + migrations.RemoveField( + model_name='versionperson', + name='alternative_names', + ), + ] diff --git a/apis_ontology/migrations/0047_rename_alternative_names_new_person_alternative_names_and_more.py b/apis_ontology/migrations/0047_rename_alternative_names_new_person_alternative_names_and_more.py new file mode 100644 index 0000000..e2ab8a1 --- /dev/null +++ b/apis_ontology/migrations/0047_rename_alternative_names_new_person_alternative_names_and_more.py @@ -0,0 +1,23 @@ +# Generated by Django 5.1.4 on 2024-12-06 12:31 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('apis_ontology', '0046_remove_person_alternative_names_and_more'), + ] + + operations = [ + migrations.RenameField( + model_name='person', + old_name='alternative_names_new', + new_name='alternative_names', + ), + migrations.RenameField( + model_name='versionperson', + old_name='alternative_names_new', + new_name='alternative_names', + ), + ]