From f0f1ce5018b1b70935f2082991e1f86a0f9b9f19 Mon Sep 17 00:00:00 2001 From: "Waylon S. Walker" Date: Wed, 28 Jun 2023 09:11:49 -0500 Subject: [PATCH] wip --- learn_sql_model/api/hero.py | 2 +- learn_sql_model/config.py | 37 ++++++++++++++++------------------ learn_sql_model/game/player.py | 2 +- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/learn_sql_model/api/hero.py b/learn_sql_model/api/hero.py index 25c41d5..4ae0c8c 100644 --- a/learn_sql_model/api/hero.py +++ b/learn_sql_model/api/hero.py @@ -83,5 +83,5 @@ async def get_heros( ) -> Heros: "get all heros" statement = select(Hero) - heros = session.execute(statement).all() + heros = session.exec(statement).all() return Heros(__root__=heros) diff --git a/learn_sql_model/config.py b/learn_sql_model/config.py index 07a7b4a..9619314 100644 --- a/learn_sql_model/config.py +++ b/learn_sql_model/config.py @@ -27,7 +27,6 @@ class ApiClient(BaseModel): protocol: str = "https" url: str = f"{protocol}://{host}" - class Database: def __init__(self, config: "Config" = None) -> None: if config is None: @@ -41,19 +40,22 @@ class Database: "transactions": None, } self.db_state = ContextVar("db_state", default=self.db_state_default.copy()) + + self.db_conf = {} + if 'sqlite' in self.config.database_url: + self.db_conf = { + 'connect_args': {"check_same_thread": False}, + 'pool_recycle': 3600, + 'pool_pre_ping': True, + } + self._engine = create_engine( + self.config.database_url, + **self.db_conf + ) @property def engine(self) -> "Engine": - try: - return self._engine - except AttributeError: - self._engine = create_engine( - self.config.database_url, - connect_args={"check_same_thread": False}, - pool_recycle=3600, - pool_pre_ping=True, - ) - return self._engine + return self._engine @property def session(self) -> "Session": @@ -99,19 +101,14 @@ def get_config(overrides: dict = {}) -> Config: config = get_config() -engine = create_engine( - config.database_url, - connect_args={"check_same_thread": False}, - pool_recycle=3600, - pool_pre_ping=True, -) +database = get_database() -SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=database.engine) def get_session() -> "Session": - with Session(engine) as session: - yield SessionLocal() + with Session(database.engine) as session: + yield session async def reset_db_state(config: Config = None) -> None: diff --git a/learn_sql_model/game/player.py b/learn_sql_model/game/player.py index a4b84d7..f5db3f3 100644 --- a/learn_sql_model/game/player.py +++ b/learn_sql_model/game/player.py @@ -16,7 +16,7 @@ class Player: self.hero = HeroCreate(**hero.dict()).post() self.game = game - self.others = Heros(heros=[]) + self.others = [] #Heros(heros=[]) self.width = 16 self.height = 16 self.white = (255, 255, 255)