Skip to content

Commit

Permalink
fix: resolve migration failure when merging several diverging issues
Browse files Browse the repository at this point in the history
After migrations were on hold for a while, merging back several issues with migrations caused a migration error. This was caused by sligtly wrong order of procedures (possibly as a result of an automatic merge) but also an oversight on my part - as the compound identifiers are loaded from compounds_manual.csv during LHS data loading I didn't think to test with RHS data, but evidently this partly touches the same models and filled the tables with incompatible data types. This patch should resolve these issues without a data loss.
  • Loading branch information
kaliif committed Nov 6, 2024
1 parent 02daf2f commit d91810d
Show file tree
Hide file tree
Showing 8 changed files with 3 additions and 90 deletions.
44 changes: 3 additions & 41 deletions viewer/migrations/0074_auto_20241018_1028.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,66 +19,28 @@ def temporary_name(apps, schema_editor):
def reverse_temporary_name(apps, schema_editor):
pass

def relink_type(apps, schema_editor):
CompoundIdentifier = apps.get_model('viewer', 'CompoundIdentifier')
CompoundIdentifierType = apps.get_model('viewer', 'CompoundIdentifierType')

for idf in CompoundIdentifier.objects.all():
idf.type = CompoundIdentifierType.objects.get(name=idf.name_type)
idf.save()

def reverse_relink_type(apps, schema_editor):
pass

# with my edits:
# this contains some edits, the automatically constructed
# migration was not in the correct order, so I had to move things
# around to preserve data
operations = [
# migrations.RunPython(lambda apps, schema_editor: print('first migration')),
# migrations.AlterField(
# model_name='compoundidentifiertype',
# name='name',
# field=models.TextField(unique=True),
# ),
# add new foreign key field to CompoundIdentifier
# migrations.RunPython(lambda apps, schema_editor: print('second migration')),
# migrations.AddField(
# model_name='compoundidentifier',
# name='name_type',
# field=models.ForeignKey(null=True, to_field='name', on_delete=django.db.models.deletion.CASCADE, to='viewer.compoundidentifiertype'),
# ),
migrations.AddField(
model_name='compoundidentifier',
name='name_type',
field=models.TextField(null=True),
),
# populate values in new field
migrations.RunPython(lambda apps, schema_editor: print('third migration, data')),
migrations.RunPython(temporary_name, reverse_temporary_name),

# continue with the original migration, remove field, set the new pk
migrations.RunPython(lambda apps, schema_editor: print('fourth migration')),
migrations.RemoveField(
model_name='compoundidentifier',
name='type',
),
migrations.RunPython(lambda apps, schema_editor: print('fifth migration')),
migrations.RemoveField(
model_name='compoundidentifiertype',
name='id',
),
# migrations.AlterField(
# model_name='compoundidentifier',
# name='name',
# field=models.TextField(),
# ),
migrations.RunPython(lambda apps, schema_editor: print('6th migration')),
migrations.AlterField(
model_name='compoundidentifiertype',
name='name',
field=models.TextField(primary_key=True, serialize=False),
),
# migrations.RemoveField(
# model_name='compoundidentifiertype',
# name='id',
# ),

]
5 changes: 0 additions & 5 deletions viewer/migrations/0075_remove_compoundidentifier_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ def reverse_relink_type(apps, schema_editor):
pass

operations = [
migrations.RunPython(lambda apps, schema_editor: print('migration 0075')),
# migrations.RemoveField(
# model_name='compoundidentifier',
# name='type',
# ),
migrations.AddField(
model_name='compoundidentifier',
name='type',
Expand Down
12 changes: 0 additions & 12 deletions viewer/migrations/0076_compoundidentifier_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@ class Migration(migrations.Migration):
]

operations = [
# migrations.AddField(
# model_name='compoundidentifier',
# name='type',
# field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='viewer.compoundidentifiertype'),
# ),
# continuing the migration tweak that started in 0074
migrations.RunPython(lambda apps, schema_editor: print('migration 0076')),
# migrations.RenameField(
# model_name='compoundidentifier',
# old_name='name_type',
# new_name='type',
# ),
migrations.RemoveField(
model_name='compoundidentifier',
name='name_type',
Expand Down
1 change: 0 additions & 1 deletion viewer/migrations/0077_alter_compoundidentifier_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class Migration(migrations.Migration):
]

operations = [
migrations.RunPython(lambda apps, schema_editor: print('migration 0077')),
migrations.AlterField(
model_name='compoundidentifier',
name='type',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class Migration(migrations.Migration):
]

operations = [
migrations.RunPython(lambda apps, schema_editor: print('migration 0078')),
migrations.AddConstraint(
model_name='compoundidentifier',
constraint=models.UniqueConstraint(fields=('type', 'compound', 'name'), name='unique_compoundidentifier'),
Expand Down
28 changes: 0 additions & 28 deletions viewer/migrations/0079_auto_20241023_1439.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,6 @@ class Migration(migrations.Migration):
('viewer', '0078_compoundidentifier_unique_compoundidentifier'),
]

# def temporary_name(apps, schema_editor):
# Compound = apps.get_model('viewer', 'Compound')

# for compound in Compound.objects.all():
# compound.id_current_identifier = compound.type.name
# compound.save()

# def reverse_temporary_name(apps, schema_editor):
# pass

# def relink_type(apps, schema_editor):
# CompoundIdentifier = apps.get_model('viewer', 'CompoundIdentifier')
# CompoundIdentifierType = apps.get_model('viewer', 'CompoundIdentifierType')

# for idf in CompoundIdentifier.objects.all():
# idf.type = CompoundIdentifierType.objects.get(name=idf.name_type)
# idf.save()

# def reverse_relink_type(apps, schema_editor):
# pass


def move_contents(apps, schema_editor):
Compound = apps.get_model('viewer', 'Compound')
Expand All @@ -45,18 +24,11 @@ def reverse_move_contents(apps, schema_editor):


operations = [
migrations.RunPython(lambda apps, schema_editor: print('migration 0079, first')),
migrations.RemoveField(
model_name='compound',
name='all_identifiers',
),

migrations.RunPython(lambda apps, schema_editor: print('migration 0079, second')),
# migrations.AddField(
# model_name='compoundid',
# name='name_current_identifier',
# field=models.TextField(null=True),
# ),
migrations.RunPython(move_contents, reverse_move_contents),
migrations.RemoveField(
model_name='compound',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class Migration(migrations.Migration):
]

operations = [
migrations.RunPython(lambda apps, schema_editor: print('migration 0080')),
migrations.AlterField(
model_name='compoundidentifier',
name='compound',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class Migration(migrations.Migration):
]

operations = [
migrations.RunPython(lambda apps, schema_editor: print('migration 0081')),
migrations.AlterField(
model_name='compound',
name='current_identifier',
Expand Down

0 comments on commit d91810d

Please sign in to comment.