Skip to content

Commit

Permalink
Merge pull request #889 from aiidateam/fix_for_old_wf_in_sqla_issue_883
Browse files Browse the repository at this point in the history
Fix for issue #883
  • Loading branch information
szoupanos authored Nov 7, 2017
2 parents 2dad741 + 6731502 commit 36c2a13
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
9 changes: 6 additions & 3 deletions aiida/backends/sqlalchemy/models/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,22 @@ def get_or_create(self, **kwargs): # this is to emulate the django method

def set_value(self, arg):
from aiida.orm import Node
from aiida.backends.sqlalchemy import get_scoped_session
try:
if isinstance(arg, Node) or issubclass(arg.__class__, Node):
if arg.pk is None:
raise ValueError("Cannot add an unstored node as an attribute of a Workflow!")
self.aiida_obj = arg.dbnode
sess = get_scoped_session()
self.aiida_obj = sess.merge(arg.dbnode, load=True)
self.value_type = wf_data_value_types.AIIDA
self.save()
else:
self.json_value = json.dumps(arg)
self.value_type = wf_data_value_types.JSON
self.save()
except:
raise ValueError("Cannot set the parameter {}".format(self.name))
except Exception as ex:
raise ValueError("Cannot set the parameter {}\n".format(self.name)
+ ex.message)

def get_value(self):
if self.value_type == wf_data_value_types.JSON:
Expand Down
47 changes: 46 additions & 1 deletion aiida/backends/sqlalchemy/tests/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from aiida.common.utils import get_configured_user_email



class TestSessionSqla(AiidaTestCase):
"""
The following tests check that the session works as expected in some
Expand Down Expand Up @@ -181,3 +180,49 @@ def test_session_update_and_expiration_4(self):
code.store()

self.drop_connection()

def test_session_wfdata(self):
"""
This test checks that the
aiida.backends.sqlalchemy.models.workflow.DbWorkflowData#set_value
method works as expected. There were problems with the DbNode object
that was added as a value to a DbWorkflowData object. If there was
an older version of the dbnode in the session than the one given to
to the DbWorkflowData#set_value then there was a collision in the
session and SQLA identity map.
"""
from aiida.orm.node import Node
from aiida.workflows.test import WFTestSimpleWithSubWF
from aiida.backends.sqlalchemy import get_scoped_session
from aiida.orm.utils import load_node

# Create a node and store it
n = Node()
n.store()

# Keep some useful information
n_id = n.dbnode.id
old_dbnode = n.dbnode

# Get the session
sess = get_scoped_session()
# Remove everything from the session
sess.expunge_all()

# Create a workflow and store it
wf = WFTestSimpleWithSubWF()
wf.store()

# Load a new version of the node
n_reloaded = load_node(n_id)

# Remove everything from the session
sess.expunge_all()

# Add the dbnode that was firstly added to the session
sess.add(old_dbnode)

# Add as attribute the node that was added after the first cleanup
# of the session
# At this point the following command should not fail
wf.add_attribute('a', n_reloaded)
3 changes: 2 additions & 1 deletion aiida/orm/implementation/sqlalchemy/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from aiida.backends.sqlalchemy.models.workflow import DbWorkflow, DbWorkflowStep
from aiida.backends.utils import get_automatic_user
from aiida.common import aiidalogger
from aiida.common.datastructures import wf_states, wf_exit_call
from aiida.common.datastructures import (wf_states, wf_exit_call,
wf_default_call)
from aiida.common.exceptions import (InternalError, ModificationNotAllowed,
NotExistent, ValidationError,
AiidaException)
Expand Down

0 comments on commit 36c2a13

Please sign in to comment.