wip
This commit is contained in:
parent
d747299c95
commit
f0f1ce5018
3 changed files with 19 additions and 22 deletions
|
|
@ -83,5 +83,5 @@ async def get_heros(
|
||||||
) -> Heros:
|
) -> Heros:
|
||||||
"get all heros"
|
"get all heros"
|
||||||
statement = select(Hero)
|
statement = select(Hero)
|
||||||
heros = session.execute(statement).all()
|
heros = session.exec(statement).all()
|
||||||
return Heros(__root__=heros)
|
return Heros(__root__=heros)
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,6 @@ class ApiClient(BaseModel):
|
||||||
protocol: str = "https"
|
protocol: str = "https"
|
||||||
url: str = f"{protocol}://{host}"
|
url: str = f"{protocol}://{host}"
|
||||||
|
|
||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
def __init__(self, config: "Config" = None) -> None:
|
def __init__(self, config: "Config" = None) -> None:
|
||||||
if config is None:
|
if config is None:
|
||||||
|
|
@ -42,18 +41,21 @@ class Database:
|
||||||
}
|
}
|
||||||
self.db_state = ContextVar("db_state", default=self.db_state_default.copy())
|
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
|
@property
|
||||||
def engine(self) -> "Engine":
|
def engine(self) -> "Engine":
|
||||||
try:
|
return self._engine
|
||||||
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
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def session(self) -> "Session":
|
def session(self) -> "Session":
|
||||||
|
|
@ -99,19 +101,14 @@ def get_config(overrides: dict = {}) -> Config:
|
||||||
|
|
||||||
|
|
||||||
config = get_config()
|
config = get_config()
|
||||||
engine = create_engine(
|
database = get_database()
|
||||||
config.database_url,
|
|
||||||
connect_args={"check_same_thread": False},
|
|
||||||
pool_recycle=3600,
|
|
||||||
pool_pre_ping=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=database.engine)
|
||||||
|
|
||||||
|
|
||||||
def get_session() -> "Session":
|
def get_session() -> "Session":
|
||||||
with Session(engine) as session:
|
with Session(database.engine) as session:
|
||||||
yield SessionLocal()
|
yield session
|
||||||
|
|
||||||
|
|
||||||
async def reset_db_state(config: Config = None) -> None:
|
async def reset_db_state(config: Config = None) -> None:
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ class Player:
|
||||||
self.hero = HeroCreate(**hero.dict()).post()
|
self.hero = HeroCreate(**hero.dict()).post()
|
||||||
|
|
||||||
self.game = game
|
self.game = game
|
||||||
self.others = Heros(heros=[])
|
self.others = [] #Heros(heros=[])
|
||||||
self.width = 16
|
self.width = 16
|
||||||
self.height = 16
|
self.height = 16
|
||||||
self.white = (255, 255, 255)
|
self.white = (255, 255, 255)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue