From 158b9f0a705ce0ec6a1068f778dc8065f86640f0 Mon Sep 17 00:00:00 2001 From: Sergey Golitsynskiy Date: Wed, 16 Mar 2022 16:48:33 -0400 Subject: [PATCH] s/eagerload/joinedload (SQLAlchemy 2.0) Ref: https://docs.sqlalchemy.org/en/14/orm/loading_relationships.html?highlight=eagerload#sqlalchemy.orm.eagerload --- lib/galaxy/managers/history_contents.py | 16 ++++++++-------- lib/galaxy/webapps/galaxy/controllers/history.py | 13 ++++++------- lib/galaxy/webapps/galaxy/controllers/page.py | 4 ++-- .../webapps/galaxy/controllers/visualization.py | 4 ++-- .../webapps/galaxy/controllers/workflow.py | 5 ++--- lib/galaxy/webapps/reports/controllers/system.py | 4 ++-- scripts/cleanup_datasets/cleanup_datasets.py | 6 +++--- 7 files changed, 25 insertions(+), 27 deletions(-) diff --git a/lib/galaxy/managers/history_contents.py b/lib/galaxy/managers/history_contents.py index d5f5eab2f69..ae98f3b02d8 100644 --- a/lib/galaxy/managers/history_contents.py +++ b/lib/galaxy/managers/history_contents.py @@ -19,7 +19,7 @@ from sqlalchemy import ( true, ) from sqlalchemy.orm import ( - eagerload, + joinedload, undefer, ) @@ -407,9 +407,9 @@ class HistoryContentsManager(base.SortableManager): .query(component_class) .filter(component_class.id.in_(id_list)) .options(undefer(component_class._metadata)) - .options(eagerload("dataset.actions")) # TODO: use class attr after moving Dataset to declarative mapping. - .options(eagerload(component_class.tags)) - .options(eagerload(component_class.annotations)) + .options(joinedload("dataset.actions")) # TODO: use class attr after moving Dataset to declarative mapping. + .options(joinedload(component_class.tags)) + .options(joinedload(component_class.annotations)) ) return {row.id: row for row in query.all()} @@ -422,9 +422,9 @@ class HistoryContentsManager(base.SortableManager): self._session() .query(component_class) .filter(component_class.id.in_(id_list)) - .options(eagerload(component_class.collection)) - .options(eagerload(component_class.tags)) - .options(eagerload(component_class.annotations)) + .options(joinedload(component_class.collection)) + .options(joinedload(component_class.tags)) + .options(joinedload(component_class.annotations)) ) # This will conditionally join a potentially costly job_state summary @@ -432,7 +432,7 @@ class HistoryContentsManager(base.SortableManager): # should really be a property of the manager class instance if serialization_params and serialization_params.keys: if "job_state_summary" in serialization_params.keys: - query = query.options(eagerload(component_class.job_state_summary)) + query = query.options(joinedload(component_class.job_state_summary)) return {row.id: row for row in query.all()} diff --git a/lib/galaxy/webapps/galaxy/controllers/history.py b/lib/galaxy/webapps/galaxy/controllers/history.py index b6d1e77f768..679e0844f72 100644 --- a/lib/galaxy/webapps/galaxy/controllers/history.py +++ b/lib/galaxy/webapps/galaxy/controllers/history.py @@ -7,7 +7,6 @@ from sqlalchemy import ( true, ) from sqlalchemy.orm import ( - eagerload, joinedload, undefer, ) @@ -264,14 +263,14 @@ class HistoryAllPublishedGrid(grids.Grid): def build_initial_query(self, trans, **kwargs): # TODO: Tags are still loaded one at a time, consider doing this all at once: - # - eagerload would keep everything in one query but would explode the number of rows and potentially + # - joinedload would keep everything in one query but would explode the number of rows and potentially # result in unneeded info transferred over the wire. # - subqueryload("tags").subqueryload("tag") would probably be better under postgres but I'd # like some performance data against a big database first - might cause problems? # - Pull down only username from associated User table since that is all that is used - # (can be used during search). Need join in addition to the eagerload since it is used in - # the .count() query which doesn't respect the eagerload options (could eliminate this with #5523). + # (can be used during search). Need join in addition to the joinedload since it is used in + # the .count() query which doesn't respect the joinedload options (could eliminate this with #5523). # - Undefer average_rating column to prevent loading individual ratings per-history. # - Eager load annotations - this causes a left join which might be inefficient if there were # potentially many items per history (like if joining HDAs for instance) but there should only @@ -279,7 +278,7 @@ class HistoryAllPublishedGrid(grids.Grid): return ( trans.sa_session.query(self.model_class) .join("user") - .options(eagerload("user").load_only("username"), eagerload("annotations"), undefer("average_rating")) + .options(joinedload("user").load_only("username"), joinedload("annotations"), undefer("average_rating")) ) def apply_query_filter(self, trans, query, **kwargs): @@ -685,8 +684,8 @@ class HistoryController(BaseUIController, SharableMixin, UsesAnnotations, UsesIt user = session.query(model.User).filter_by(username=username).first() history = ( trans.sa_session.query(model.History) - .options(eagerload("tags")) - .options(eagerload("annotations")) + .options(joinedload("tags")) + .options(joinedload("annotations")) .filter_by(user=user, slug=slug, deleted=False) .first() ) diff --git a/lib/galaxy/webapps/galaxy/controllers/page.py b/lib/galaxy/webapps/galaxy/controllers/page.py index 4ba9fc15f71..1c6e9c74960 100644 --- a/lib/galaxy/webapps/galaxy/controllers/page.py +++ b/lib/galaxy/webapps/galaxy/controllers/page.py @@ -5,7 +5,7 @@ from sqlalchemy import ( true, ) from sqlalchemy.orm import ( - eagerload, + joinedload, undefer, ) @@ -166,7 +166,7 @@ class PageAllPublishedGrid(grids.Grid): trans.sa_session.query(self.model_class) .join("user") .filter(model.User.deleted == false()) - .options(eagerload("user").load_only("username"), eagerload("annotations"), undefer("average_rating")) + .options(joinedload("user").load_only("username"), joinedload("annotations"), undefer("average_rating")) ) def apply_query_filter(self, trans, query, **kwargs): diff --git a/lib/galaxy/webapps/galaxy/controllers/visualization.py b/lib/galaxy/webapps/galaxy/controllers/visualization.py index 97865af03d6..b9df3334d7a 100644 --- a/lib/galaxy/webapps/galaxy/controllers/visualization.py +++ b/lib/galaxy/webapps/galaxy/controllers/visualization.py @@ -16,7 +16,7 @@ from sqlalchemy import ( true, ) from sqlalchemy.orm import ( - eagerload, + joinedload, undefer, ) @@ -290,7 +290,7 @@ class VisualizationAllPublishedGrid(grids.Grid): return ( trans.sa_session.query(self.model_class) .join("user") - .options(eagerload("user").load_only("username"), eagerload("annotations"), undefer("average_rating")) + .options(joinedload("user").load_only("username"), joinedload("annotations"), undefer("average_rating")) ) def apply_query_filter(self, trans, query, **kwargs): diff --git a/lib/galaxy/webapps/galaxy/controllers/workflow.py b/lib/galaxy/webapps/galaxy/controllers/workflow.py index 43160e314fe..a1664254d4b 100644 --- a/lib/galaxy/webapps/galaxy/controllers/workflow.py +++ b/lib/galaxy/webapps/galaxy/controllers/workflow.py @@ -10,7 +10,6 @@ from sqlalchemy import ( desc, ) from sqlalchemy.orm import ( - eagerload, joinedload, lazyload, undefer, @@ -174,8 +173,8 @@ class StoredWorkflowAllPublishedGrid(grids.Grid): .join("user") .options( lazyload("latest_workflow"), - eagerload("user").load_only("username"), - eagerload("annotations"), + joinedload("user").load_only("username"), + joinedload("annotations"), undefer("average_rating"), ) ) diff --git a/lib/galaxy/webapps/reports/controllers/system.py b/lib/galaxy/webapps/reports/controllers/system.py index 6fad63d995d..0c8bfbe63ee 100644 --- a/lib/galaxy/webapps/reports/controllers/system.py +++ b/lib/galaxy/webapps/reports/controllers/system.py @@ -13,7 +13,7 @@ from sqlalchemy import ( null, true, ) -from sqlalchemy.orm import eagerload +from sqlalchemy.orm import joinedload from galaxy import ( model, @@ -115,7 +115,7 @@ class System(BaseUIController): model.History.update_time < cutoff_time, ) ) - .options(eagerload("datasets")) + .options(joinedload("datasets")) ) for history in histories: diff --git a/scripts/cleanup_datasets/cleanup_datasets.py b/scripts/cleanup_datasets/cleanup_datasets.py index 7a3c2c7f32e..8d5834b3b4f 100755 --- a/scripts/cleanup_datasets/cleanup_datasets.py +++ b/scripts/cleanup_datasets/cleanup_datasets.py @@ -19,7 +19,7 @@ from sqlalchemy import ( null, true, ) -from sqlalchemy.orm import eagerload +from sqlalchemy.orm import joinedload sys.path.insert(1, os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, "lib"))) @@ -258,7 +258,7 @@ def purge_histories(app, cutoff_time, remove_from_disk, info_only=False, force_r histories = ( app.sa_session.query(app.model.History) .filter(and_(app.model.History.table.c.deleted == true(), app.model.History.update_time < cutoff_time)) - .options(eagerload("datasets")) + .options(joinedload("datasets")) ) else: histories = ( @@ -270,7 +270,7 @@ def purge_histories(app, cutoff_time, remove_from_disk, info_only=False, force_r app.model.History.update_time < cutoff_time, ) ) - .options(eagerload("datasets")) + .options(joinedload("datasets")) ) for history in histories: log.info("### Processing history id %d (%s)", history.id, unicodify(history.name))