Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move database access code out of galaxy.util #16526

Merged
merged 6 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/galaxy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from galaxy.managers.api_keys import ApiKeyManager
from galaxy.managers.citations import CitationsManager
from galaxy.managers.collections import DatasetCollectionManager
from galaxy.managers.dbkeys import GenomeBuilds
from galaxy.managers.folders import FolderManager
from galaxy.managers.hdas import HDAManager
from galaxy.managers.histories import HistoryManager
Expand Down Expand Up @@ -126,7 +127,6 @@
listify,
StructuredExecutionTimer,
)
from galaxy.util.dbkeys import GenomeBuilds
from galaxy.util.task import IntervalTask
from galaxy.util.tool_shed import tool_shed_registry
from galaxy.visualization.data_providers.registry import DataProviderRegistry
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/app_unittest_utils/galaxy_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from galaxy.job_metrics import JobMetrics
from galaxy.jobs.manager import NoopManager
from galaxy.managers.collections import DatasetCollectionManager
from galaxy.managers.dbkeys import GenomeBuilds
from galaxy.managers.hdas import HDAManager
from galaxy.managers.histories import HistoryManager
from galaxy.managers.jobs import JobSearch
Expand Down Expand Up @@ -51,7 +52,6 @@
from galaxy.tools.data import ToolDataTableManager
from galaxy.util import StructuredExecutionTimer
from galaxy.util.bunch import Bunch
from galaxy.util.dbkeys import GenomeBuilds
from galaxy.web.short_term_storage import (
ShortTermStorageAllocator,
ShortTermStorageConfiguration,
Expand Down
12 changes: 8 additions & 4 deletions lib/galaxy/util/dbkeys.py → lib/galaxy/managers/dbkeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import os.path
import re
from json import loads
from typing import (
Dict,
List,
)

from galaxy.util import (
galaxy_directory,
Expand All @@ -17,9 +21,9 @@

def read_dbnames(filename):
"""Read build names from file"""
db_names = []
db_names: List = []
nsoranzo marked this conversation as resolved.
Show resolved Hide resolved
try:
ucsc_builds = {}
ucsc_builds: Dict = {}
nsoranzo marked this conversation as resolved.
Show resolved Hide resolved
man_builds = [] # assume these are integers
name_to_db_base = {}
if filename is None:
Expand All @@ -43,9 +47,9 @@ def read_dbnames(filename):
ucsc_builds[db_base] = []
name_to_db_base[fields[1]] = db_base
# we want to sort within a species numerically by revision number
build_rev = re.compile(r"\d+$")
pattern = re.compile(r"\d+$")
try:
build_rev = int(build_rev.findall(fields[0])[0])
build_rev = int(pattern.findall(fields[0])[0])
except Exception:
build_rev = 0
ucsc_builds[db_base].append((build_rev, fields[0], fields[1]))
Expand Down
13 changes: 13 additions & 0 deletions lib/galaxy/managers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
and_,
exc,
func,
select,
true,
)
from sqlalchemy.orm.exc import NoResultFound
Expand Down Expand Up @@ -837,3 +838,15 @@ def _add_parsers(self):
)

self.fn_filter_parsers.update({})


def get_user_by_username(session, user_class, username):
"""
Get a user from the database by username.
(We pass the session and the user_class to accommodate usage from the tool_shed app.)
"""
try:
stmt = select(user_class).filter(user_class.username == username)
return session.execute(stmt).scalar_one()
except Exception:
return None
2 changes: 1 addition & 1 deletion lib/galaxy/structured_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from galaxy.di import Container
from galaxy.files import ConfiguredFileSources
from galaxy.job_metrics import JobMetrics
from galaxy.managers.dbkeys import GenomeBuilds
from galaxy.model.base import (
ModelMapping,
SharedModelMapping,
Expand All @@ -32,7 +33,6 @@
from galaxy.tool_util.deps.containers import ContainerFinder
from galaxy.tool_util.deps.views import DependencyResolversView
from galaxy.tool_util.verify import test_data
from galaxy.util.dbkeys import GenomeBuilds
from galaxy.util.tool_shed.tool_shed_registry import Registry as ToolShedRegistry
from galaxy.web_stack import ApplicationStack
from galaxy.webhooks import WebhooksRegistry
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/tools/parameters/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from galaxy import util
from galaxy.files import ProvidesUserFileSourcesUserContext
from galaxy.managers import dbkeys
from galaxy.model import (
cached_id,
Dataset,
Expand All @@ -33,7 +34,6 @@
from galaxy.schema.fetch_data import FilesPayload
from galaxy.tool_util.parser import get_input_source as ensure_input_source
from galaxy.util import (
dbkeys,
sanitize_param,
string_as_bool,
string_as_bool_or_none,
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/tools/remote_tool_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from galaxy.files import ConfiguredFileSources
from galaxy.job_execution.compute_environment import SharedComputeEnvironment
from galaxy.job_execution.setup import JobIO
from galaxy.managers.dbkeys import GenomeBuilds
from galaxy.metadata.set_metadata import (
get_metadata_params,
get_object_store,
Expand All @@ -30,7 +31,6 @@
ToolDataTableManager,
)
from galaxy.util.bunch import Bunch
from galaxy.util.dbkeys import GenomeBuilds


class ToolAppConfig(NamedTuple):
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/tools/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import tempfile
from contextlib import contextmanager

from galaxy.managers.dbkeys import GenomeBuilds
from galaxy.tools.data import ToolDataTableManager
from galaxy.util.bunch import Bunch
from galaxy.util.dbkeys import GenomeBuilds


class ValidationContext:
Expand Down
11 changes: 0 additions & 11 deletions lib/galaxy/util/tool_shed/common_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,6 @@ def get_tool_shed_repository_url(app: HasToolShedRegistry, tool_shed: str, owner
return tool_shed_url


def get_user_by_username(app, username):
"""Get a user from the database by username."""
sa_session = app.model.session
try:
user = sa_session.query(app.model.User).filter(app.model.User.table.c.username == username).one()
return user
except Exception:
return None


def handle_galaxy_url(trans, **kwd):
galaxy_url = kwd.get("galaxy_url", None)
if galaxy_url:
Expand Down Expand Up @@ -303,7 +293,6 @@ def remove_protocol_from_tool_shed_url(tool_shed_url: str) -> str:
"get_tool_shed_repository_ids",
"get_tool_shed_url_from_tool_shed_registry",
"get_tool_shed_repository_url",
"get_user_by_username",
"handle_galaxy_url",
"handle_tool_shed_url_protocol",
"parse_repository_dependency_tuple",
Expand Down
2 changes: 0 additions & 2 deletions lib/tool_shed/util/common_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
get_tool_shed_repository_ids,
get_tool_shed_repository_url,
get_tool_shed_url_from_tool_shed_registry,
get_user_by_username,
handle_galaxy_url,
handle_tool_shed_url_protocol,
parse_repository_dependency_tuple,
Expand All @@ -30,7 +29,6 @@
"get_tool_shed_repository_ids",
"get_tool_shed_url_from_tool_shed_registry",
"get_tool_shed_repository_url",
"get_user_by_username",
"handle_galaxy_url",
"handle_tool_shed_url_protocol",
"parse_repository_dependency_tuple",
Expand Down
2 changes: 1 addition & 1 deletion lib/tool_shed/webapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from galaxy.config import configure_logging
from galaxy.managers.api_keys import ApiKeyManager
from galaxy.managers.citations import CitationsManager
from galaxy.managers.dbkeys import GenomeBuilds
from galaxy.managers.users import UserManager
from galaxy.model.base import SharedModelMapping
from galaxy.model.tags import CommunityTagHandler
Expand All @@ -27,7 +28,6 @@
)
from galaxy.security import idencoding
from galaxy.structured_app import BasicSharedApp
from galaxy.util.dbkeys import GenomeBuilds
from galaxy.web_stack import application_stack_instance
from tool_shed.grids.repository_grid_filter_manager import RepositoryGridFilterManager
from tool_shed.structured_app import ToolShedApp
Expand Down
5 changes: 3 additions & 2 deletions lib/tool_shed/webapp/controllers/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
util,
web,
)
from galaxy.managers.users import get_user_by_username
from galaxy.model.base import transaction
from galaxy.tool_shed.util import dependency_display
from galaxy.tools.repositories import ValidationContext
Expand Down Expand Up @@ -2292,7 +2293,7 @@ def set_malicious(self, trans, id, ctx_str, **kwd):
def sharable_owner(self, trans, owner):
"""Support for sharable URL for each repository owner's tools, e.g. http://example.org/view/owner."""
try:
user = common_util.get_user_by_username(trans, owner)
user = get_user_by_username(trans.model.session, trans.model.User, owner)
except Exception:
user = None
if user:
Expand Down Expand Up @@ -2320,7 +2321,7 @@ def sharable_repository(self, trans, owner, name):
else:
# If the owner is valid, then show all of their repositories.
try:
user = common_util.get_user_by_username(trans, owner)
user = get_user_by_username(trans.model.session, trans.model.User, owner)
except Exception:
user = None
if user:
Expand Down