Skip to content

Commit 9478513

Browse files
authored
Merge pull request galaxyproject#22462 from mvdbeek/warnings/sqlalchemy-cleanup
Warnings/sqlalchemy cleanup
2 parents 6a80aa7 + b4732d0 commit 9478513

17 files changed

Lines changed: 60 additions & 44 deletions

File tree

conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def pytest_configure(config):
2+
try:
3+
import sqlalchemy.exc # noqa: F401
4+
except ImportError:
5+
pass
6+
else:
7+
config.addinivalue_line("filterwarnings", "error::sqlalchemy.exc.SAWarning")

lib/galaxy/job_execution/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def from_json(cls, path, sa_session):
157157
# Drop in 24.0
158158
io_dict.pop("model_class", None)
159159
job_id = io_dict.pop("job_id")
160-
job = sa_session.query(Job).get(job_id)
160+
job = sa_session.get(Job, job_id)
161161
return cls(sa_session=sa_session, job=job, **io_dict)
162162

163163
@classmethod

lib/galaxy/managers/folders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,5 +573,5 @@ def get_folder(session, folder_id):
573573

574574

575575
def get_count(session, statement):
576-
stmt = select(func.count()).select_from(statement)
576+
stmt = select(func.count()).select_from(statement.subquery())
577577
return session.scalar(stmt)

lib/galaxy/managers/histories.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,5 +1091,5 @@ def username_contains(self, item, val: str) -> bool:
10911091

10921092

10931093
def get_count(session, statement):
1094-
stmt = select(func.count()).select_from(statement)
1094+
stmt = select(func.count()).select_from(statement.subquery())
10951095
return session.scalar(stmt)

lib/galaxy/managers/history_contents.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def copy_contents(self, trans, history_id, payload: CopyDatasetsPayload) -> Copy
230230
decoded_target_ids.extend(self.app.security.decode_id(h) for h in payload.target_history_ids)
231231

232232
target_histories = [
233-
h for h in map(trans.sa_session.query(model.History).get, decoded_target_ids) if h and h.user == user
233+
h for h in (trans.sa_session.get(model.History, i) for i in decoded_target_ids) if h and h.user == user
234234
]
235235
if len(target_histories) != len(decoded_target_ids):
236236
raise glx_exceptions.MessageException("You lack permission for one or more destination histories.")
@@ -244,8 +244,8 @@ def copy_contents(self, trans, history_id, payload: CopyDatasetsPayload) -> Copy
244244
else:
245245
raise glx_exceptions.MessageException("Unknown content type.")
246246

247-
contents = list(map(trans.sa_session.query(model.HistoryDatasetAssociation).get, decoded_ds))
248-
contents.extend(map(trans.sa_session.query(model.HistoryDatasetCollectionAssociation).get, decoded_dc))
247+
contents = [trans.sa_session.get(model.HistoryDatasetAssociation, i) for i in decoded_ds]
248+
contents.extend(trans.sa_session.get(model.HistoryDatasetCollectionAssociation, i) for i in decoded_dc)
249249
contents.sort(key=lambda c: c.hid)
250250

251251
for c in contents:

lib/galaxy/managers/pages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,5 +703,5 @@ def _build_page_query(select_clause, user: User, slug: str):
703703

704704

705705
def get_count(session, statement):
706-
stmt = select(func.count()).select_from(statement)
706+
stmt = select(func.count()).select_from(statement.subquery())
707707
return session.scalar(stmt)

lib/galaxy/managers/visualizations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,5 @@ def add_deserializers(self):
217217

218218

219219
def get_count(session, statement):
220-
stmt = select(func.count()).select_from(statement)
220+
stmt = select(func.count()).select_from(statement.subquery())
221221
return session.scalar(stmt)

lib/galaxy/model/__init__.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -916,8 +916,8 @@ class User(Base, Dictifiable, RepresentById):
916916
data_manager_histories: Mapped[list["DataManagerHistoryAssociation"]] = relationship(back_populates="user")
917917
roles: Mapped[list["UserRoleAssociation"]] = relationship(back_populates="user")
918918
stored_workflows: Mapped[list["StoredWorkflow"]] = relationship(
919-
back_populates="user",
920919
primaryjoin=(lambda: User.id == StoredWorkflow.user_id),
920+
viewonly=True,
921921
)
922922
all_notifications: Mapped[list["UserNotificationAssociation"]] = relationship(back_populates="user")
923923

@@ -3522,7 +3522,9 @@ class History(Base, HasTags, Dictifiable, UsesAnnotations, HasName, Serializable
35223522
archive_export_id: Mapped[Optional[int]] = mapped_column(ForeignKey("store_export_association.id"), default=None)
35233523

35243524
datasets: Mapped[list["HistoryDatasetAssociation"]] = relationship(
3525-
back_populates="history", order_by=lambda: asc(HistoryDatasetAssociation.hid)
3525+
primaryjoin=(lambda: HistoryDatasetAssociation.history_id == History.id),
3526+
order_by=lambda: asc(HistoryDatasetAssociation.hid),
3527+
viewonly=True,
35263528
)
35273529
exports: Mapped[list["JobExportHistoryArchive"]] = relationship(
35283530
back_populates="history",
@@ -3635,7 +3637,7 @@ def __init__(self, id=None, name=None, user=None):
36353637
self.user = user
36363638
# Objects to eventually add to history
36373639
self._pending_additions = []
3638-
self._item_by_hid_cache = None
3640+
self._copied_from_object_id_cache = None
36393641

36403642
@reconstructor
36413643
def init_on_load(self):
@@ -3828,15 +3830,21 @@ def copy(self, name=None, target_user=None, activatable=False, all_datasets=Fals
38283830
hdas = self.datasets
38293831
else:
38303832
hdas = self.active_datasets
3833+
copied_from_object_id_map = {}
38313834
for hda in hdas:
38323835
# Copy HDA.
38333836
new_hda = hda.copy(flush=False)
38343837
new_history.add_dataset(new_hda, set_hid=False, quota=applies_to_quota)
3838+
copied_from_object_id_map[hda.id] = new_hda
38353839

38363840
if target_user:
38373841
new_hda.copy_item_annotation(db_session, self.user, hda, target_user, new_hda)
38383842
new_hda.copy_tags_from(target_user, hda)
38393843

3844+
# Pre-populate cache so HDCA copy's minimize_copies can find
3845+
# the just-created HDAs (viewonly self.datasets won't see unflushed rows).
3846+
new_history._copied_from_object_id_cache = copied_from_object_id_map
3847+
38403848
# Copy history dataset collections
38413849
if all_datasets:
38423850
hdcas = self.dataset_collections
@@ -3861,10 +3869,10 @@ def copy(self, name=None, target_user=None, activatable=False, all_datasets=Fals
38613869

38623870
return new_history
38633871

3864-
def get_dataset_by_hid(self, hid):
3865-
if self._item_by_hid_cache is None:
3866-
self._item_by_hid_cache = {dataset.hid: dataset for dataset in self.datasets}
3867-
return self._item_by_hid_cache.get(hid)
3872+
def get_copied_dataset(self, id):
3873+
if self._copied_from_object_id_cache is None:
3874+
return None
3875+
return self._copied_from_object_id_cache.get(id)
38683876

38693877
@property
38703878
def has_possible_members(self):
@@ -7955,7 +7963,7 @@ def build_statement():
79557963
subq = subq1.union(subq2)
79567964

79577965
# Build and return final query
7958-
stm = select().select_from(subq)
7966+
stm = select().select_from(subq.subquery())
79597967
# Add aggregate columns for each job state
79607968
for state in enum_values(Job.states):
79617969
col = func.sum(case((column(state_label) == state, 1), else_=0)).label(state)
@@ -8434,12 +8442,8 @@ def copy_to_collection(
84348442
elif isinstance(element_object, HistoryDatasetAssociation):
84358443
new_element_object = None
84368444
if minimize_copies:
8437-
new_element_object = element_destination.get_dataset_by_hid(element_object.hid)
8438-
if (
8439-
new_element_object
8440-
and new_element_object.dataset
8441-
and new_element_object.dataset.id == element_object.dataset_id
8442-
):
8445+
new_element_object = element_destination.get_copied_dataset(element_object.id)
8446+
if new_element_object:
84438447
element_object = new_element_object
84448448
else:
84458449
new_element_object = element_object.copy(
@@ -8593,7 +8597,7 @@ class StoredWorkflow(Base, HasTags, Dictifiable, RepresentById, UsesCreateAndUpd
85938597
published: Mapped[Optional[bool]] = mapped_column(index=True, default=False)
85948598

85958599
user: Mapped["User"] = relationship(
8596-
primaryjoin=(lambda: User.id == StoredWorkflow.user_id), back_populates="stored_workflows"
8600+
primaryjoin=(lambda: User.id == StoredWorkflow.user_id),
85978601
)
85988602
workflows: Mapped[list["Workflow"]] = relationship(
85998603
back_populates="stored_workflow",
@@ -8794,7 +8798,7 @@ class Workflow(Base, Dictifiable, RepresentById):
87948798
parent_workflow_steps = relationship(
87958799
"WorkflowStep",
87968800
primaryjoin=(lambda: Workflow.id == WorkflowStep.subworkflow_id),
8797-
back_populates="subworkflow",
8801+
viewonly=True,
87988802
)
87998803
stored_workflow = relationship(
88008804
"StoredWorkflow",
@@ -9007,7 +9011,6 @@ class WorkflowStep(Base, RepresentById, UsesCreateAndUpdateTime):
90079011

90089012
subworkflow: Mapped[Optional["Workflow"]] = relationship(
90099013
primaryjoin=(lambda: Workflow.id == WorkflowStep.subworkflow_id),
9010-
back_populates="parent_workflow_steps",
90119014
)
90129015
dynamic_tool: Mapped[Optional["DynamicTool"]] = relationship(
90139016
primaryjoin=(lambda: DynamicTool.id == WorkflowStep.dynamic_tool_id)
@@ -11432,7 +11435,7 @@ def user_exists(cls, *args, **kwargs):
1143211435
(Required by social_core.storage.UserMixin interface)
1143311436
"""
1143411437
stmt_user = select(User).filter_by(*args, **kwargs)
11435-
stmt_count = select(func.count()).select_from(stmt_user)
11438+
stmt_count = select(func.count()).select_from(stmt_user.subquery())
1143611439
return cls.sa_session.scalar(stmt_count) > 0
1143711440

1143811441
@classmethod
@@ -12922,7 +12925,7 @@ class Credential(Base):
1292212925
_metadata=deferred(HistoryDatasetAssociation.table.c._metadata),
1292312926
dependent_jobs=relationship(JobToInputDatasetAssociation, back_populates="dataset"),
1292412927
creating_job_associations=relationship(JobToOutputDatasetAssociation, back_populates="dataset"),
12925-
history=relationship(History, back_populates="datasets"),
12928+
history=relationship(History),
1292612929
implicitly_converted_datasets=relationship(
1292712930
ImplicitlyConvertedDatasetAssociation,
1292812931
primaryjoin=(lambda: ImplicitlyConvertedDatasetAssociation.hda_parent_id == HistoryDatasetAssociation.id),

lib/galaxy/model/custom_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ def process_result_value(self, value, dialect):
132132
class MutableJSONType(JSONType):
133133
"""Associated with MutationObj"""
134134

135+
cache_ok = True
136+
135137

136138
class MutationObj(Mutable):
137139
"""

lib/galaxy/model/security.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1790,7 +1790,7 @@ def _get_valid_roles_exposed(session, search_query, is_admin, limit, page, page_
17901790
)
17911791
stmt = stmt.union(stmt2)
17921792

1793-
count_stmt = select(func.count()).select_from(stmt)
1793+
count_stmt = select(func.count()).select_from(stmt.subquery())
17941794
total_count = session.scalar(count_stmt)
17951795

17961796
stmt = stmt.order_by(Role.name)

0 commit comments

Comments
 (0)