Skip to content

Commit

Permalink
Fix SA2.0 ORM usage in model.store
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavcs committed Sep 28, 2023
1 parent 85e6fe4 commit 51c3731
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
35 changes: 18 additions & 17 deletions lib/galaxy/model/store/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ?
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/model/store/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 51c3731

Please sign in to comment.