Skip to content

Commit

Permalink
Ensure version when adding and including to Housekeeper (#2880)(patch)
Browse files Browse the repository at this point in the history
Currently, when adding and including a file to Housekeeper through the Housekeeper API, a version is fetched using `last_version()` and an error is raised if the version is missing. This is changed now as the version is ensured using `get_create_version()` instead and removing the exception raising.

### Changed

- Use `get_create_version()` instead of `last_version()`
- Removed error raise
- Renamed `get_create_version` to `get_or_create_version`
  • Loading branch information
diitaz93 authored Jan 30, 2024
1 parent d6009e0 commit 78b2fa9
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 14 deletions.
13 changes: 3 additions & 10 deletions cg/apps/housekeeper/hk.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
from housekeeper.include import checksum as hk_checksum
from housekeeper.include import include_version
from housekeeper.store import Store, models
from housekeeper.store.database import (
create_all_tables,
drop_all_tables,
initialize_database,
)
from housekeeper.store.database import create_all_tables, drop_all_tables, initialize_database
from housekeeper.store.models import Archive, Bundle, File, Version
from sqlalchemy.orm import Query

Expand Down Expand Up @@ -281,7 +277,7 @@ def get_latest_bundle_version(self, bundle_name: str) -> Version | None:
LOG.debug(f"Found Housekeeper version object for {bundle_name}: {repr(last_version)}")
return last_version

def get_create_version(self, bundle_name: str) -> Version:
def get_or_create_version(self, bundle_name: str) -> Version:
"""Returns the latest version of a bundle if it exists. If not creates a bundle and
returns its version."""
last_version: Version = self.last_version(bundle=bundle_name)
Expand Down Expand Up @@ -352,10 +348,7 @@ def add_and_include_file_to_latest_version(
self, bundle_name: str, file: Path, tags: list
) -> None:
"""Adds and includes a file in the latest version of a bundle."""
version: Version = self.last_version(bundle_name)
if not version:
LOG.warning(f"Bundle: {bundle_name} not found in Housekeeper")
raise HousekeeperBundleVersionMissingError
version: Version = self.get_or_create_version(bundle_name)
hk_file: File = self.add_file(version_obj=version, tags=tags, path=str(file.absolute()))
self.include_file(version_obj=version, file_obj=hk_file)
self.commit()
Expand Down
2 changes: 1 addition & 1 deletion cg/meta/transfer/external_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def add_transfer_to_housekeeper(
sample_internal_id=sample.internal_id
)
)
last_version: Version = self.housekeeper_api.get_create_version(
last_version: Version = self.housekeeper_api.get_or_create_version(
bundle_name=sample.internal_id
)
fastq_paths_to_add: list[Path] = self.get_fastq_paths_to_add(
Expand Down
4 changes: 2 additions & 2 deletions tests/apps/hk/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ def test_get_create_version(
version_obj = populated_housekeeper_api.bundle(case_id).versions[0]

# When the case_id is given the function should return its latest version
latest_version = populated_housekeeper_api.get_create_version(case_id)
latest_version = populated_housekeeper_api.get_or_create_version(case_id)

# Then assert that the versions match
assert version_obj == latest_version

# When a case_id not present in hk is given.
latest_version = populated_housekeeper_api.get_create_version(another_case_id)
latest_version = populated_housekeeper_api.get_or_create_version(another_case_id)

# Then assert the new bundle is created the version is new.
assert latest_version.bundle.name == another_case_id
2 changes: 1 addition & 1 deletion tests/mocks/hk_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def version(self, *args, **kwargs):
"""Fetch a version"""
return self._version_obj

def get_create_version(self, bundle_name: str):
def get_or_create_version(self, bundle_name: str):
"""Returns the latest version of a bundle if it exists. If no creates a bundle and returns its version"""
last_version = self.last_version(bundle=bundle_name)
if not last_version:
Expand Down

0 comments on commit 78b2fa9

Please sign in to comment.