Skip to content

Commit

Permalink
Move function to model.base to avoid circular import
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavcs committed Dec 5, 2023
1 parent ab2f76c commit 631e84d
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 17 deletions.
6 changes: 4 additions & 2 deletions lib/galaxy/managers/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@
WorkflowInvocation,
WorkflowInvocationStep,
)
from galaxy.model.base import transaction
from galaxy.model.database_utils import ensure_object_added_to_session
from galaxy.model.base import (
ensure_object_added_to_session,
transaction,
)
from galaxy.model.index_filter_util import (
append_user_filter,
raw_text_column_filter,
Expand Down
6 changes: 4 additions & 2 deletions lib/galaxy/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@
import galaxy.model.tags
import galaxy.security.passwords
import galaxy.util
from galaxy.model.base import transaction
from galaxy.model.base import (
ensure_object_added_to_session,
transaction,
)
from galaxy.model.custom_types import (
DoubleEncodedJsonType,
JSONType,
Expand All @@ -129,7 +132,6 @@
UUIDType,
)
from galaxy.model.database_object_names import NAMING_CONVENTION
from galaxy.model.database_utils import ensure_object_added_to_session
from galaxy.model.item_attrs import (
get_item_annotation_str,
UsesAnnotations,
Expand Down
20 changes: 20 additions & 0 deletions lib/galaxy/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from sqlalchemy import event
from sqlalchemy.orm import (
object_session,
scoped_session,
Session,
sessionmaker,
Expand Down Expand Up @@ -167,3 +168,22 @@ def before_flush(session, flush_context, instances):
obj.__create_version__(session)
for obj in versioned_objects(session.deleted):
obj.__create_version__(session, deleted=True)


def ensure_object_added_to_session(object_to_add, *, object_in_session=None, session=None) -> bool:
"""
This function is intended as a safeguard to mimic pre-SQLAlchemy 2.0 behavior.
`object_to_add` was implicitly merged into a Session prior to SQLAlchemy 2.0, which was indicated
by `RemovedIn20Warning` warnings logged while running Galaxy's tests. (See https://github.com/galaxyproject/galaxy/issues/12541)
As part of the upgrade to 2.0, the `cascade_backrefs=False` argument was added to the relevant relationships that turned off this behavior.
This function is called from the code that triggered these warnings, thus emulating the cascading behavior.
The intention is to remove all such calls, as well as this function definition, after the move to SQLAlchemy 2.0.
# Ref: https://docs.sqlalchemy.org/en/14/changelog/migration_14.html#cascade-backrefs-behavior-deprecated-for-removal-in-2-0
"""
if session:
session.add(object_to_add)
return True
if object_in_session and object_session(object_in_session):
object_session(object_in_session).add(object_to_add)
return True
return False
6 changes: 4 additions & 2 deletions lib/galaxy/model/store/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@
ProvidesUserFileSourcesUserContext,
)
from galaxy.files.uris import stream_url_to_file
from galaxy.model.base import transaction
from galaxy.model.database_utils import ensure_object_added_to_session
from galaxy.model.base import (
ensure_object_added_to_session,
transaction,
)
from galaxy.model.mapping import GalaxyModelMapping
from galaxy.model.metadata import MetadataCollection
from galaxy.model.orm.util import (
Expand Down
6 changes: 4 additions & 2 deletions lib/galaxy/webapps/base/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
from galaxy.managers import context
from galaxy.managers.session import GalaxySessionManager
from galaxy.managers.users import UserManager
from galaxy.model.base import transaction
from galaxy.model.database_utils import ensure_object_added_to_session
from galaxy.model.base import (
ensure_object_added_to_session,
transaction,
)
from galaxy.structured_app import (
BasicSharedApp,
MinimalApp,
Expand Down
6 changes: 4 additions & 2 deletions lib/galaxy/workflow/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
exceptions,
model,
)
from galaxy.model.base import transaction
from galaxy.model.database_utils import ensure_object_added_to_session
from galaxy.model.base import (
ensure_object_added_to_session,
transaction,
)
from galaxy.tool_util.parser import ToolOutputCollectionPart
from galaxy.tools.parameters.basic import (
DataCollectionToolParameter,
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/workflow/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
WorkflowStep,
WorkflowStepConnection,
)
from galaxy.model.database_utils import ensure_object_added_to_session
from galaxy.model.base import ensure_object_added_to_session
from galaxy.model.dataset_collections import matching
from galaxy.schema.invocation import (
CancelReason,
Expand Down
6 changes: 4 additions & 2 deletions lib/galaxy/workflow/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
WorkflowInvocation,
WorkflowInvocationStep,
)
from galaxy.model.base import transaction
from galaxy.model.database_utils import ensure_object_added_to_session
from galaxy.model.base import (
ensure_object_added_to_session,
transaction,
)
from galaxy.schema.invocation import (
CancelReason,
FailureReason,
Expand Down
6 changes: 4 additions & 2 deletions lib/galaxy/workflow/run_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
WorkflowRequestInputParameter,
WorkflowRequestStepState,
)
from galaxy.model.base import transaction
from galaxy.model.database_utils import ensure_object_added_to_session
from galaxy.model.base import (
ensure_object_added_to_session,
transaction,
)
from galaxy.tools.parameters.meta import expand_workflow_inputs
from galaxy.workflow.resources import get_resource_mapper_function

Expand Down
6 changes: 4 additions & 2 deletions test/integration/test_job_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
from sqlalchemy import select

from galaxy import model
from galaxy.model.base import transaction
from galaxy.model.database_utils import ensure_object_added_to_session
from galaxy.model.base import (
ensure_object_added_to_session,
transaction,
)
from galaxy_test.base import api_asserts
from galaxy_test.base.populators import DatasetPopulator
from galaxy_test.driver import integration_util
Expand Down

0 comments on commit 631e84d

Please sign in to comment.