From 3142c9df9a1c4039566d9f79615de66ddbd75bc7 Mon Sep 17 00:00:00 2001 From: John Davis Date: Fri, 29 Sep 2023 14:58:04 -0400 Subject: [PATCH] Fix SA2.0 ORM usage in model.History.disk_size; simplify --- lib/galaxy/model/__init__.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/lib/galaxy/model/__init__.py b/lib/galaxy/model/__init__.py index 7cb4d467a649..47b0619edef2 100644 --- a/lib/galaxy/model/__init__.py +++ b/lib/galaxy/model/__init__.py @@ -3273,23 +3273,17 @@ def disk_size(self): all non-purged, unique datasets within it. """ # non-.expression part of hybrid.hybrid_property: called when an instance is the namespace (not the class) - db_session = object_session(self) - rval = db_session.query( - func.sum( - db_session.query(HistoryDatasetAssociation.dataset_id, Dataset.total_size) - .join(Dataset) - .filter(HistoryDatasetAssociation.table.c.history_id == self.id) - .filter(HistoryDatasetAssociation.purged != true()) - .filter(Dataset.purged != true()) - # unique datasets only - .distinct() - .subquery() - .c.total_size - ) - ).first()[0] - if rval is None: - rval = 0 - return rval + subq = ( + select(HistoryDatasetAssociation.dataset_id, Dataset.total_size) + .join(Dataset) + .where(HistoryDatasetAssociation.table.c.history_id == self.id) + .where(HistoryDatasetAssociation.purged != true()) + .where(Dataset.purged != true()) + .distinct() # unique datasets only + .subquery() + ) + stmt = select(func.sum(subq.c.total_size)) + return object_session(self).scalar(stmt) or 0 @disk_size.expression # type: ignore[no-redef] def disk_size(cls):