|
1 | | -# pylint: disable=attribute-defined-outside-init, ungrouped-imports |
| 1 | +# pylint: disable=attribute-defined-outside-init |
2 | 2 | from __future__ import annotations |
3 | 3 | import abc |
4 | | -from typing import TYPE_CHECKING |
5 | | -from sqlalchemy import create_engine |
6 | | -from sqlalchemy.orm import sessionmaker |
7 | | - |
8 | | -from allocation import config |
| 4 | +from typing import Callable |
| 5 | +from sqlalchemy.orm.session import Session |
9 | 6 | from allocation.adapters import repository |
10 | 7 | from . import messagebus |
11 | 8 |
|
12 | | -if TYPE_CHECKING: |
13 | | - from sqlalchemy.orm.session import Session |
14 | 9 |
|
| 10 | +SessionFactory = Callable[[], Session] |
15 | 11 |
|
16 | 12 |
|
17 | 13 | class AbstractUnitOfWork(abc.ABC): |
@@ -44,18 +40,13 @@ def rollback(self): |
44 | 40 |
|
45 | 41 |
|
46 | 42 |
|
47 | | -DEFAULT_SESSION_FACTORY = sessionmaker(bind=create_engine( |
48 | | - config.get_postgres_uri(), |
49 | | - isolation_level="SERIALIZABLE", |
50 | | -)) |
51 | | - |
52 | 43 | class SqlAlchemyUnitOfWork(AbstractUnitOfWork): |
53 | 44 |
|
54 | | - def __init__(self, session_factory=DEFAULT_SESSION_FACTORY): |
| 45 | + def __init__(self, session_factory: SessionFactory): |
55 | 46 | self.session_factory = session_factory |
56 | 47 |
|
57 | 48 | def __enter__(self): |
58 | | - self.session = self.session_factory() # type: Session |
| 49 | + self.session = self.session_factory() |
59 | 50 | self.products = repository.SqlAlchemyRepository(self.session) |
60 | 51 | return super().__enter__() |
61 | 52 |
|
|
0 commit comments