diff --git a/Makefile b/Makefile index 389c4ab..793584e 100644 --- a/Makefile +++ b/Makefile @@ -6,13 +6,13 @@ run-local: poetry run uvicorn src.api.app:app --host 0.0.0.0 --port 8000 --reload --reload-dir . --log-config=log_config.ini --log-level=debug build: - poetry run sudo docker compose -f docker-compose.$(for).yml build + poetry run docker-compose -f docker-compose.$(for).yml build start: - poetry run sudo docker compose -f docker-compose.$(for).yml up --force-recreate --remove-orphans + poetry run docker-compose -f docker-compose.$(for).yml up --force-recreate --remove-orphans up: - poetry run sudo docker compose -f docker-compose.$(for).yml up --force-recreate --remove-orphans -d + poetry run docker-compose -f docker-compose.$(for).yml up --force-recreate --remove-orphans -d stop: poetry run docker-compose -f docker-compose.$(for).yml stop diff --git a/src/repositories/database/base.py b/src/repositories/database/base.py index e17dd4c..0d1d07d 100644 --- a/src/repositories/database/base.py +++ b/src/repositories/database/base.py @@ -8,6 +8,15 @@ from src.core.config import settings from src.models.base import Base +engine = create_async_engine( + url=settings.db_url, + echo=settings.echo, +) +sessionmaker = async_sessionmaker( + bind=engine, + expire_on_commit=False, +) + @dataclass class BaseDatabaseRepository(ABC): @@ -22,34 +31,30 @@ class DatabaseRepositories(ABC): @dataclass class SQLAlchemyRepository(BaseDatabaseRepository): - engine = create_async_engine(url=settings.db_url, echo=settings.echo, pool_pre_ping=True) - sessionmaker = async_sessionmaker(engine, expire_on_commit=False) - async def add(self, obj: Base) -> None: - async with self.sessionmaker() as session: + async with sessionmaker() as session: session.add(obj) await session.commit() - await self.engine.dispose() async def merge(self, obj: Base) -> None: - async with self.sessionmaker() as session: + async with sessionmaker() as session: await session.merge(obj) async def execute(self, statement: Executable) -> None: - async with self.sessionmaker() as session: + async with sessionmaker() as session: await session.execute(statement) async def get(self, table: type[Base], primary_key: Any): # type: ignore[no-untyped-def] - async with self.sessionmaker() as session: + async with sessionmaker() as session: model = await session.get(table, primary_key) return model async def scalar(self, statement: Executable) -> Any: - async with self.sessionmaker() as session: + async with sessionmaker() as session: item = await session.scalar(statement) return item async def scalars(self, statement: Executable) -> Iterable: - async with self.sessionmaker() as session: + async with sessionmaker() as session: items = await session.scalars(statement) return items diff --git a/tests/conftest.py b/tests/conftest.py index f2d6648..a1c47cd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ import pytest from fastapi.testclient import TestClient + from src.api.app import app