-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
] |
21 changes: 21 additions & 0 deletions
21
apis_ontology/migrations/0046_remove_person_alternative_names_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
), | ||
] |
23 changes: 23 additions & 0 deletions
23
...ntology/migrations/0047_rename_alternative_names_new_person_alternative_names_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
), | ||
] |