Skip to content

Commit

Permalink
chore: migrate data to new alternative_names field
Browse files Browse the repository at this point in the history
uses three migrations to copy data from one field to the other,
rename the field and delete the old one.
  • Loading branch information
sennierer committed Dec 6, 2024
1 parent 7a0605b commit 07cb7eb
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
7 changes: 6 additions & 1 deletion apis_ontology/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -19,6 +22,8 @@
# helpers
#########

ABSTRACT_ENTITY_FILTERS_EXCLUDE += ["alternative_names"]


def remove_quotes(token):
return token.strip('"')
Expand Down
42 changes: 42 additions & 0 deletions apis_ontology/migrations/0045_auto_20241206_1028.py
Original file line number Diff line number Diff line change
@@ -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),
]
Original file line number Diff line number Diff line change
@@ -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',
),
]
Original file line number Diff line number Diff line change
@@ -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',
),
]

0 comments on commit 07cb7eb

Please sign in to comment.