Coverage for app/ddd/infrastructure/database/db.py: 100%
15 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-15 01:44 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-15 01:44 +0000
1import os
2from collections.abc import Generator
3from contextlib import contextmanager
5from sqlmodel import Session, create_engine
7db_name = os.environ["SQLALCHEMY_DATABASE"]
9__db_urls = {
10 "sqlite": os.environ["SQLALCHEMY_DATABASE_SQLITE_URI"],
11 "postgresql": os.environ["SQLALCHEMY_DATABASE_POSTGRES_URI"],
12}
14url = __db_urls[db_name]
16db_engine = create_engine(
17 url=url,
18 echo=False, # SQL見る場合に有効化する
19)
21# 手動実行用途で分離
22@contextmanager
23def get_session_context() -> Generator[Session]:
24 with Session(db_engine) as session:
25 yield session
27def get_session() -> Generator[Session]:
28 with get_session_context() as session:
29 yield session