Skip to content

Commit

Permalink
Remove any use of our ORM in the migrations and tests
Browse files Browse the repository at this point in the history
Many of the migrations and their respective unit tests used the ORM of
`aiida-core` or the database model classes directly. However, as the
database models evolve and change, during migrations one cannot rely on
the current implementation of the database models or the ORM as it may
not be commensurate of their state at the time of the migration. This
means that the migrations absolutely have to be written relying only on
raw SQL or on the model classes that are provided through the migration
mechanism of the various backends, as they will make sure that those
fake classes reflect the state of the models at the time of the migration.
  • Loading branch information
sphuber committed Mar 4, 2019
1 parent 1ba85b2 commit 30b1253
Show file tree
Hide file tree
Showing 28 changed files with 1,066 additions and 623 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
aiida/backends/tests/cmdline/commands/test_node.py|
aiida/backends/tests/cmdline/commands/test_user.py|
aiida/backends/tests/cmdline/commands/test_work.py|
aiida/backends/tests/test_computer.py|
aiida/backends/tests/test_dataclasses.py|
aiida/backends/tests/test_dbimporters.py|
aiida/backends/tests/test_export_and_import.py|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from django.db import migrations

from aiida.backends.djsite.db.migrations import upgrade_schema_version
from aiida.backends.general.migrations.utils import load_numpy_array_from_repository
from . import ModelModifierV0025

REVISION = '1.0.26'
Expand All @@ -30,35 +31,27 @@

def create_trajectory_symbols_attribute(apps, _):
"""Create the symbols attribute from the repository array for all `TrajectoryData` nodes."""
from aiida.orm import load_node

DbNode = apps.get_model('db', 'DbNode')
DbAttribute = apps.get_model('db', 'DbAttribute')

modifier = ModelModifierV0025(DbAttribute)
modifier = ModelModifierV0025(apps, DbAttribute)

DbNode = apps.get_model('db', 'DbNode')
trajectories_pk = DbNode.objects.filter(type='node.data.array.trajectory.TrajectoryData.').values_list(
'id', flat=True)
for t_pk in trajectories_pk:
trajectory = load_node(t_pk)
symbols = trajectory.get_array('symbols').tolist()
modifier.set_value_for_node(DbNode.objects.get(pk=trajectory.pk), 'symbols', symbols)
nodes = DbNode.objects.filter(type='node.data.array.trajectory.TrajectoryData.').values_list('id', 'uuid')
for pk, uuid in nodes:
symbols = load_numpy_array_from_repository(uuid, 'symbols').tolist()
modifier.set_value_for_node(DbNode.objects.get(pk=pk), 'symbols', symbols)


def delete_trajectory_symbols_attribute(apps, _):
"""Delete the symbols attribute for all `TrajectoryData` nodes."""
from aiida.orm import load_node

DbNode = apps.get_model('db', 'DbNode')
DbAttribute = apps.get_model('db', 'DbAttribute')

modifier = ModelModifierV0025(DbAttribute)
modifier = ModelModifierV0025(apps, DbAttribute)

DbNode = apps.get_model('db', 'DbNode')
trajectories_pk = DbNode.objects.filter(type='node.data.array.trajectory.TrajectoryData.').values_list(
'id', flat=True)
for t_pk in trajectories_pk:
trajectory = load_node(t_pk)
modifier.del_value_for_node(DbNode.objects.get(pk=trajectory.pk), 'symbols')
nodes = DbNode.objects.filter(type='node.data.array.trajectory.TrajectoryData.').values_list('id', flat=True)
for pk in nodes:
modifier.del_value_for_node(DbNode.objects.get(pk=pk), 'symbols')


class Migration(migrations.Migration):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from django.db import migrations

from aiida.backends.djsite.db.migrations import upgrade_schema_version
from aiida.backends.general.migrations import utils
from . import ModelModifierV0025

REVISION = '1.0.27'
Expand All @@ -30,44 +31,31 @@

def delete_trajectory_symbols_array(apps, _):
"""Delete the symbols array from all `TrajectoryData` nodes."""
from aiida.orm import load_node

DbNode = apps.get_model('db', 'DbNode')
DbAttribute = apps.get_model('db', 'DbAttribute')

modifier = ModelModifierV0025(DbAttribute)
modifier = ModelModifierV0025(apps, DbAttribute)

DbNode = apps.get_model('db', 'DbNode')
trajectories_pk = DbNode.objects.filter(type='node.data.array.trajectory.TrajectoryData.').values_list(
'id', flat=True)
for t_pk in trajectories_pk:
trajectory = load_node(t_pk)
modifier.del_value_for_node(DbNode.objects.get(pk=trajectory.pk), 'array|symbols')
trajectory.delete_object('symbols.npy', force=True)
nodes = DbNode.objects.filter(type='node.data.array.trajectory.TrajectoryData.').values_list('id', 'uuid')
for pk, uuid in nodes:
modifier.del_value_for_node(DbNode.objects.get(pk=pk), 'array|symbols')
utils.delete_numpy_array_from_repository(uuid, 'symbols')


def create_trajectory_symbols_array(apps, _):
"""Create the symbols array for all `TrajectoryData` nodes."""
import numpy
import tempfile
from aiida.orm import load_node

DbNode = apps.get_model('db', 'DbNode')
DbAttribute = apps.get_model('db', 'DbAttribute')

modifier = ModelModifierV0025(DbAttribute)
modifier = ModelModifierV0025(apps, DbAttribute)

DbNode = apps.get_model('db', 'DbNode')
trajectories_pk = DbNode.objects.filter(type='node.data.array.trajectory.TrajectoryData.').values_list(
'id', flat=True)
for t_pk in trajectories_pk:
trajectory = load_node(t_pk)
symbols = numpy.array(trajectory.get_attribute('symbols'))
# Save the .npy file (using set_array raises ModificationNotAllowed error)
with tempfile.NamedTemporaryFile() as handle:
numpy.save(handle, symbols)
handle.flush()
handle.seek(0)
trajectory.put_object_from_filelike(handle, 'symbols.npy')
modifier.set_value_for_node(DbNode.objects.get(pk=trajectory.pk), 'array|symbols', list(symbols.shape))
nodes = DbNode.objects.filter(type='node.data.array.trajectory.TrajectoryData.').values_list('id', 'uuid')
for pk, uuid in nodes:
symbols = numpy.array(modifier.get_value_for_node(pk, 'symbols'))
utils.store_numpy_array_in_repository(uuid, 'symbols', symbols)
modifier.set_value_for_node(DbNode.objects.get(pk=pk), 'array|symbols', list(symbols.shape))


class Migration(migrations.Migration):
Expand Down
Loading

0 comments on commit 30b1253

Please sign in to comment.