Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dmkjfs committed Dec 3, 2024
1 parent 3e5adec commit 208c8f3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 15 additions & 10 deletions src/repositories/database/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from fastapi.testclient import TestClient

from src.api.app import app


Expand Down

0 comments on commit 208c8f3

Please sign in to comment.