Add "root_execution" mapped property to WorkflowExecution model

* We need to avoid using direct DB queries w/o using a mapped
  model where possible for performance sake. Using a mapped entity
  property is always more efficient because it's cached in an
  SQLAlchemy session.

Change-Id: I2d7652ea0cff8f2db7259d285ac98c582bf15b62
This commit is contained in:
Renat Akhmerov 2019-03-18 10:41:59 +07:00
parent a73e3defea
commit 32c96b1b6c
3 changed files with 13 additions and 7 deletions

View File

@ -353,6 +353,13 @@ WorkflowExecution.root_execution_id = sa.Column(
nullable=True
)
WorkflowExecution.root_execution = relationship(
WorkflowExecution,
remote_side=WorkflowExecution.id,
lazy='select'
)
# Many-to-one for 'TaskExecution' and 'WorkflowExecution'.
TaskExecution.workflow_execution_id = sa.Column(

View File

@ -453,9 +453,11 @@ class SubworkflowsTest(base.EngineTestCase):
wf2_ex = db_api.get_workflow_execution(wf2_ex.id)
wf3_ex = db_api.get_workflow_execution(wf3_ex.id)
self.assertIsNone(wf1_ex.root_execution_id, None)
self.assertEqual(wf2_ex.root_execution_id, wf1_ex.id)
self.assertEqual(wf3_ex.root_execution_id, wf1_ex.id)
self.assertIsNone(wf1_ex.root_execution_id, None)
self.assertEqual(wf2_ex.root_execution_id, wf1_ex.id)
self.assertEqual(wf2_ex.root_execution, wf1_ex)
self.assertEqual(wf3_ex.root_execution_id, wf1_ex.id)
self.assertEqual(wf3_ex.root_execution, wf1_ex)
def test_cascade_delete(self):
wf_text = """

View File

@ -17,7 +17,6 @@ from oslo_config import cfg
from oslo_log import log as logging
from mistral import context as auth_ctx
from mistral.db.v2 import api as db_api
from mistral.db.v2.sqlalchemy import models
from mistral import exceptions as exc
from mistral import expressions as expr
@ -333,9 +332,7 @@ def get_workflow_environment_dict(wf_ex):
return {}
if wf_ex.root_execution_id:
return get_workflow_environment_dict(
db_api.get_workflow_execution(wf_ex.root_execution_id)
)
return get_workflow_environment_dict(wf_ex.root_execution)
env_dict = wf_ex.params['env'] if 'env' in wf_ex.params else {}