"""
This module contains the UnitOfWork Abstract Base Class.
All UnitOfWork implementations to conform to this interface.
"""
import logging
from abc import ABC, abstractmethod
from ska_db_oda.repository import (
EntityRelationshipRepository,
ExecutionBlockRepository,
PanelDecisionRepository,
PanelRepository,
PanelReviewRepository,
ProjectRepository,
ProposalAccessRepository,
ProposalRepository,
SBDefinitionRepository,
SBInstanceRepository,
)
LOGGER = logging.getLogger(__name__)
[docs]
class AbstractUnitOfWork(ABC):
"""Provides the interface to store or retrieve a group of OSO Entities.
Commits or rollsback a series of database transactions as an atomic operation
"""
sbds: SBDefinitionRepository
sbis: SBInstanceRepository
ebs: ExecutionBlockRepository
prjs: ProjectRepository
prsls: ProposalRepository
panels: PanelRepository
prslacc: ProposalAccessRepository
pnlds: PanelDecisionRepository
rvws: PanelReviewRepository
entity_relationship: EntityRelationshipRepository
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
if (exc_type, exc_value, traceback) == (None, None, None):
LOGGER.debug(
"UnitOfWork exited successfully. Rolling back any uncommitted transactions."
)
else:
LOGGER.error(
(
"UnitOfWork exited with error '%s : %s'. Rolling back any"
" uncommitted transactions."
),
exc_type,
exc_value,
)
self.rollback()
[docs]
@abstractmethod
def commit(self) -> None:
"""Commit the entities added to this Unit of Work to the database atomically.
:raises ValueError: if the validation of committed sbds or the metadata fails
"""
raise NotImplementedError
[docs]
@abstractmethod
def rollback(self) -> None:
"""Initiate the rollback of this Unit of Work.
If no commit is carried out or an error is raised,
the unit of work is rolled back to a safe state
"""
raise NotImplementedError