Skip to content

Commit

Permalink
Start migrating sqlalchemy in tool shed 2.0 to newer SA.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmchilton committed Sep 27, 2023
1 parent 439daea commit 45f2126
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 49 deletions.
39 changes: 21 additions & 18 deletions lib/tool_shed/managers/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from sqlalchemy import (
and_,
false,
select,
)

from galaxy import web
Expand Down Expand Up @@ -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


Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions lib/tool_shed/metadata/repository_metadata_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
and_,
false,
or_,
select,
)

from galaxy import util
Expand Down Expand Up @@ -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}, "
Expand Down
57 changes: 31 additions & 26 deletions lib/tool_shed/test/base/test_db_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
and_,
false,
true,
select,
)

import galaxy.model
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 45f2126

Please sign in to comment.