diff --git a/lib/tool_shed/managers/repositories.py b/lib/tool_shed/managers/repositories.py index 805f716164c5..0dcd413d7991 100644 --- a/lib/tool_shed/managers/repositories.py +++ b/lib/tool_shed/managers/repositories.py @@ -19,6 +19,7 @@ from sqlalchemy import ( and_, false, + select, ) from galaxy import web @@ -204,14 +205,14 @@ def guid_to_repository(app: ToolShedApp, tool_id: str) -> "Repository": shed, _, owner, name, rest = tool_id.split("/", 5) clause_list = [ and_( - app.model.Repository.table.c.deprecated == false(), - app.model.Repository.table.c.deleted == false(), - app.model.Repository.table.c.name == name, - app.model.User.table.c.username == owner, - app.model.Repository.table.c.user_id == app.model.User.table.c.id, + app.model.Repository.deprecated == false(), + app.model.Repository.deleted == false(), + app.model.Repository.name == name, + app.model.User.username == owner, + app.model.Repository.user_id == app.model.User.id, ) ] - repository = app.model.context.query(app.model.Repository).filter(*clause_list).first() + repository = app.model.context.scalars(select(app.model.Repository).where(*clause_list).limit(1)).first() return repository @@ -224,14 +225,16 @@ def index_tool_ids(app: ToolShedApp, tool_ids: List[str]) -> Dict[str, Any]: name = repository.name clause_list = [ and_( - app.model.Repository.table.c.deprecated == false(), - app.model.Repository.table.c.deleted == false(), - app.model.Repository.table.c.name == name, - app.model.User.table.c.username == owner, - app.model.Repository.table.c.user_id == app.model.User.table.c.id, + app.model.Repository.deprecated == false(), + app.model.Repository.deleted == false(), + app.model.Repository.name == name, + app.model.User.username == owner, + app.model.Repository.user_id == app.model.User.id, ) ] - repository = app.model.context.current.sa_session.query(app.model.Repository).filter(*clause_list).first() + repository = app.model.context.current.sa_session.scalars( + select(app.model.Repository).where(*clause_list).limit(1) + ).first() if not repository: log.warning(f"Repository {owner}/{name} does not exist, skipping") continue @@ -275,22 +278,22 @@ def index_tool_ids(app: ToolShedApp, tool_ids: List[str]) -> Dict[str, Any]: def index_repositories(app: ToolShedApp, name: Optional[str], owner: Optional[str], deleted: bool): clause_list = [ and_( - app.model.Repository.table.c.deprecated == false(), - app.model.Repository.table.c.deleted == deleted, + app.model.Repository.deprecated == false(), + app.model.Repository.deleted == deleted, ) ] if owner is not None: clause_list.append( and_( - app.model.User.table.c.username == owner, - app.model.Repository.table.c.user_id == app.model.User.table.c.id, + app.model.User.username == owner, + app.model.Repository.user_id == app.model.User.id, ) ) if name is not None: - clause_list.append(app.model.Repository.table.c.name == name) + clause_list.append(app.model.Repository.name == name) repositories = [] for repository in ( - app.model.context.query(app.model.Repository).filter(*clause_list).order_by(app.model.Repository.table.c.name) + app.model.context.query(app.model.Repository).filter(*clause_list).order_by(app.model.Repository.name) ): repositories.append(repository) return repositories diff --git a/lib/tool_shed/metadata/repository_metadata_manager.py b/lib/tool_shed/metadata/repository_metadata_manager.py index 9ec1b29271b2..fe36109b5e86 100644 --- a/lib/tool_shed/metadata/repository_metadata_manager.py +++ b/lib/tool_shed/metadata/repository_metadata_manager.py @@ -11,6 +11,7 @@ and_, false, or_, + select, ) from galaxy import util @@ -164,16 +165,14 @@ def handle_repository_elem(self, repository_elem, only_if_compiling_contained_td is_valid = False return repository_dependency_tup, is_valid, error_message try: - repository = ( - self.sa_session.query(self.app.model.Repository) - .filter( + repository = self.sa_session.execute( + select(self.app.model.Repository).where( and_( self.app.model.Repository.table.c.name == name, self.app.model.Repository.table.c.user_id == user.id, ) ) - .one() - ) + ).scalar_one() except Exception: error_message = f"Ignoring repository dependency definition for tool shed {toolshed}," error_message += f"name {name}, owner {owner}, " diff --git a/lib/tool_shed/test/base/test_db_util.py b/lib/tool_shed/test/base/test_db_util.py index 9081a007a097..b768b5802989 100644 --- a/lib/tool_shed/test/base/test_db_util.py +++ b/lib/tool_shed/test/base/test_db_util.py @@ -8,6 +8,7 @@ and_, false, true, + select, ) import galaxy.model @@ -53,42 +54,46 @@ def get_all_repositories(): def get_all_installed_repositories(session=None) -> List[galaxy.model.tool_shed_install.ToolShedRepository]: if session is None: session = install_session() - return list( - session.query(galaxy.model.tool_shed_install.ToolShedRepository) - .filter( - and_( - galaxy.model.tool_shed_install.ToolShedRepository.table.c.deleted == false(), - galaxy.model.tool_shed_install.ToolShedRepository.table.c.uninstalled == false(), - galaxy.model.tool_shed_install.ToolShedRepository.table.c.status - == galaxy.model.tool_shed_install.ToolShedRepository.installation_status.INSTALLED, + return session.scalars( + session.execute( + select(galaxy.model.tool_shed_install.ToolShedRepository).where( + and_( + galaxy.model.tool_shed_install.ToolShedRepository.deleted == false(), + galaxy.model.tool_shed_install.ToolShedRepository.uninstalled == false(), + galaxy.model.tool_shed_install.ToolShedRepository.status + == galaxy.model.tool_shed_install.ToolShedRepository.installation_status.INSTALLED, + ) ) ) - .all() - ) + ).all() def get_galaxy_repository_by_name_owner_changeset_revision(repository_name, owner, changeset_revision): - return ( - install_session() - .query(galaxy.model.tool_shed_install.ToolShedRepository) - .filter( - and_( - galaxy.model.tool_shed_install.ToolShedRepository.table.c.name == repository_name, - galaxy.model.tool_shed_install.ToolShedRepository.table.c.owner == owner, - galaxy.model.tool_shed_install.ToolShedRepository.table.c.changeset_revision == changeset_revision, + session = install_session() + return session.scalars( + session.execute( + select(galaxy.model.tool_shed_install.ToolShedRepository) + .where( + and_( + galaxy.model.tool_shed_install.ToolShedRepository.name == repository_name, + galaxy.model.tool_shed_install.ToolShedRepository.owner == owner, + galaxy.model.tool_shed_install.ToolShedRepository.changeset_revision == changeset_revision, + ) ) + .limit(1) ) - .first() - ) + ).first() def get_installed_repository_by_id(repository_id): - return ( - install_session() - .query(galaxy.model.tool_shed_install.ToolShedRepository) - .filter(galaxy.model.tool_shed_install.ToolShedRepository.table.c.id == repository_id) - .first() - ) + session = install_session() + return session.scalars( + session.execute( + select(galaxy.model.tool_shed_install.ToolShedRepository) + .where(galaxy.model.tool_shed_install.ToolShedRepository.id == repository_id) + .limit(1) + ) + ).first() def get_installed_repository_by_name_owner(repository_name, owner, return_multiple=False, session=None):