-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathmain_transaction_manager_sqla.py
More file actions
58 lines (47 loc) · 1.82 KB
/
main_transaction_manager_sqla.py
File metadata and controls
58 lines (47 loc) · 1.82 KB
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
import logging
from collections.abc import Mapping
from typing import Any, cast
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
from app.application.common.ports.transaction_manager import (
TransactionManager,
)
from app.domain.exceptions.user import UsernameAlreadyExistsError
from app.infrastructure.adapters.constants import (
DB_COMMIT_DONE,
DB_COMMIT_FAILED,
DB_CONSTRAINT_VIOLATION,
DB_FLUSH_DONE,
DB_FLUSH_FAILED,
DB_QUERY_FAILED,
)
from app.infrastructure.adapters.types import MainAsyncSession
from app.infrastructure.exceptions.gateway import DataMapperError
log = logging.getLogger(__name__)
class SqlaMainTransactionManager(TransactionManager):
def __init__(self, session: MainAsyncSession):
self._session = session
async def flush(self) -> None:
"""
:raises DataMapperError:
:raises UsernameAlreadyExists:
"""
try:
await self._session.flush()
log.debug("%s Main session.", DB_FLUSH_DONE)
except IntegrityError as error:
if "uq_users_username" in str(error):
params: Mapping[str, Any] = cast(Mapping[str, Any], error.params)
username = str(params.get("username", "unknown"))
raise UsernameAlreadyExistsError(username) from error
raise DataMapperError(DB_CONSTRAINT_VIOLATION) from error
except SQLAlchemyError as error:
raise DataMapperError(f"{DB_QUERY_FAILED} {DB_FLUSH_FAILED}") from error
async def commit(self) -> None:
"""
:raises DataMapperError:
"""
try:
await self._session.commit()
log.debug("%s Main session.", DB_COMMIT_DONE)
except SQLAlchemyError as error:
raise DataMapperError(f"{DB_QUERY_FAILED} {DB_COMMIT_FAILED}") from error