From 51c3731ba8d135480e07295f49eaf91ad7b2d655 Mon Sep 17 00:00:00 2001 From: John Davis Date: Wed, 27 Sep 2023 16:36:00 -0400 Subject: [PATCH] Fix SA2.0 ORM usage in model.store --- lib/galaxy/model/store/__init__.py | 35 +++++++++++++++--------------- lib/galaxy/model/store/discover.py | 2 +- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/galaxy/model/store/__init__.py b/lib/galaxy/model/store/__init__.py index ea10a0bca4d7..59873c02fb7c 100644 --- a/lib/galaxy/model/store/__init__.py +++ b/lib/galaxy/model/store/__init__.py @@ -39,6 +39,7 @@ WorkflowDescription, ) from rocrate.rocrate import ROCrate +from sqlalchemy import select from sqlalchemy.orm import joinedload from sqlalchemy.orm.scoping import scoped_session from sqlalchemy.sql import expression @@ -466,9 +467,8 @@ def handle_dataset_object_edit(dataset_instance, dataset_attrs): self.dataset_state_serialized = False if "id" in dataset_attrs and self.import_options.allow_edit and not self.sessionless: - dataset_instance: model.DatasetInstance = self.sa_session.query( - getattr(model, dataset_attrs["model_class"]) - ).get(dataset_attrs["id"]) + model_class = getattr(model, dataset_attrs["model_class"]) + dataset_instance: model.DatasetInstance = self.sa_session.get(model_class, dataset_attrs["id"]) attributes = [ "name", "extension", @@ -775,7 +775,7 @@ def import_folder(folder_attrs, root_folder=None): and not self.sessionless and self.import_options.allow_edit ): - library_folder = self.sa_session.query(model.LibraryFolder).get(library_attrs["id"]) + library_folder = self.sa_session.get(model.LibraryFolder, library_attrs["id"]) import_folder(library_attrs, root_folder=library_folder) else: assert self.import_options.allow_library_creation @@ -838,7 +838,7 @@ def materialize_elements(dc): dc.element_count = len(elements_attrs) if "id" in collection_attrs and self.import_options.allow_edit and not self.sessionless: - dc = self.sa_session.query(model.DatasetCollection).get(collection_attrs["id"]) + dc = self.sa_session.get(model.DatasetCollection, collection_attrs["id"]) attributes = [ "collection_type", "populated_state", @@ -865,7 +865,7 @@ def materialize_elements(dc): if "collection" in collection_attrs: dc = import_collection(collection_attrs["collection"]) if "id" in collection_attrs and self.import_options.allow_edit and not self.sessionless: - hdca = self.sa_session.query(model.HistoryDatasetCollectionAssociation).get(collection_attrs["id"]) + hdca = self.sa_session.get(model.HistoryDatasetCollectionAssociation, collection_attrs["id"]) # TODO: edit attributes... else: hdca = model.HistoryDatasetCollectionAssociation( @@ -1217,7 +1217,7 @@ def _import_jobs(self, object_import_tracker: "ObjectImportTracker", history: Op if "id" in job_attrs and not self.sessionless: # only thing we allow editing currently is associations for incoming jobs. assert self.import_options.allow_edit - job = self.sa_session.query(model.Job).get(job_attrs["id"]) + job = self.sa_session.get(model.Job, job_attrs["id"]) self._connect_job_io(job, job_attrs, _find_hda, _find_hdca, _find_dce) # type: ignore[attr-defined] self._set_job_attributes(job, job_attrs, force_terminal=False) # type: ignore[attr-defined] # Don't edit job @@ -2110,12 +2110,12 @@ def export_history( sa_session = app.model.session # Write collections' attributes (including datasets list) to file. - query = ( - sa_session.query(model.HistoryDatasetCollectionAssociation) - .filter(model.HistoryDatasetCollectionAssociation.history == history) - .filter(model.HistoryDatasetCollectionAssociation.deleted == expression.false()) + stmt = ( + select(model.HistoryDatasetCollectionAssociation) + .where(model.HistoryDatasetCollectionAssociation.history == history) + .where(model.HistoryDatasetCollectionAssociation.deleted == expression.false()) ) - collections = query.all() + collections = sa_session.scalars(stmt) for collection in collections: # filter this ? @@ -2128,15 +2128,16 @@ def export_history( # Write datasets' attributes to file. actions_backref = model.Dataset.actions # type: ignore[attr-defined] - query = ( - sa_session.query(model.HistoryDatasetAssociation) - .filter(model.HistoryDatasetAssociation.history == history) + + stmt = ( + select(model.HistoryDatasetAssociation) + .where(model.HistoryDatasetAssociation.history == history) .join(model.Dataset) .options(joinedload(model.HistoryDatasetAssociation.dataset).joinedload(actions_backref)) .order_by(model.HistoryDatasetAssociation.hid) - .filter(model.Dataset.purged == expression.false()) + .where(model.Dataset.purged == expression.false()) ) - datasets = query.all() + datasets = sa_session.scalars(stmt).unique() for dataset in datasets: dataset.annotation = get_item_annotation_str(sa_session, history.user, dataset) should_include_file = (dataset.visible or include_hidden) and (not dataset.deleted or include_deleted) diff --git a/lib/galaxy/model/store/discover.py b/lib/galaxy/model/store/discover.py index 832357acb45b..5f1a9a9b0b31 100644 --- a/lib/galaxy/model/store/discover.py +++ b/lib/galaxy/model/store/discover.py @@ -828,7 +828,7 @@ def collect_elements_for_history(elements): sa_session = ( model_persistence_context.sa_session or model_persistence_context.import_store.sa_session ) - primary_dataset = sa_session.query(galaxy.model.HistoryDatasetAssociation).get(hda_id) + primary_dataset = sa_session.get(galaxy.model.HistoryDatasetAssociation, hda_id) sources = fields_match.sources hashes = fields_match.hashes