-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_of_work.py
95 lines (73 loc) · 2.92 KB
/
unit_of_work.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from abc import ABC
from typing import Any, Generator, Iterator, List, Set, Tuple
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from kingdom_sdk import config
from kingdom_sdk.domain.aggregate import Aggregate
from kingdom_sdk.domain.exception import KingdomError
from kingdom_sdk.ports.unit_of_work import AbstractUnitOfWork
DEFAULT_SESSION_FACTORY = sessionmaker(
# ISOLATION LEVEL ENSURES aggregate's version IS RESPECTED
# That is, if version differs it will raise an exception
bind=create_engine(
config.get_database_url(),
isolation_level="REPEATABLE_READ",
echo=config.is_debug_active(),
),
autoflush=False,
)
class SQLAlchemyUnitOfWork(AbstractUnitOfWork, ABC):
"""Generic SQLAlchemy Unit of Work.
You only need to extend it and annotate the repositories types.
>>> class MyUnitOfWork(SQLAlchemyUnitOfWork):
... repository: ...
"""
_errors: List[Any]
_session_factory: sessionmaker
_session: Session
def __init__(
self, session_factory: sessionmaker = DEFAULT_SESSION_FACTORY
) -> None:
self._errors = []
self._session_factory = session_factory
def __enter__(self) -> AbstractUnitOfWork:
self._session = self._session_factory()
self._initialize_repositories(self._session)
return super().__enter__()
def __exit__(self, *args: Any) -> None:
super().__exit__(*args)
self._session.close()
def _commit(self) -> None:
self._session.commit()
def _rollback(self) -> None:
self._session.rollback()
def execute_native_statement(self, statement: str, **params: Any) -> Any:
return self._session.execute(statement, params)
def collect_new_events(self) -> Generator:
dirty: Set[Aggregate] = set()
for field_name, _ in self._repositories:
try:
repository = self.__dict__[field_name]
except KeyError as error:
raise RepositoryNotIntializedError(str(error))
if hasattr(repository, "_seen"):
dirty = dirty.union(repository._seen) # noqa
for aggregate in dirty:
while aggregate.has_events:
yield aggregate.next_event
def _initialize_repositories(self, session: Session) -> None:
for field_name, repository in self._repositories:
self.__dict__[field_name] = repository(session)
@property
def _repositories(self) -> Iterator[Tuple[str, Any]]:
return (
(field, module)
for field, module in self.__annotations__.items()
if not field.startswith("_")
)
class RepositoryNotIntializedError(KingdomError):
def __init__(self, repository_name: str) -> None:
super().__init__(
f"The repository '{repository_name}' haven't been initialized yet",
"REPOSITORY_NOT_INITIALIZED_ERROR",
)