"""
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.domain.repository import (
ExecutionBlockRepository,
ProjectRepository,
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
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:
"""Commits the entities added to this Unit of Work to the database, as an atomic transaction.
:raises ValueError: if the validation of committed sbds or the metadata fails
"""
raise NotImplementedError
[docs]
@abstractmethod
def rollback(self) -> None:
"""Initiates the rollback of this Unit of Work, so that none of the entities are persisted.
If no commit is carried out or an error is raised,
the unit of work is rolled back to a safe state
"""
raise NotImplementedError