mirror of
https://github.com/WGDashboard/WGDashboard-PRW.git
synced 2026-08-03 22:42:57 +00:00
chore: try to make a working base
This commit is contained in:
@@ -1,2 +1,43 @@
|
||||
#!/bin/env python3
|
||||
|
||||
import logging as log
|
||||
|
||||
import sqlalchemy
|
||||
import sqlalchemy.orm
|
||||
|
||||
from .schema import Base
|
||||
from .utilities import checks
|
||||
|
||||
class database():
|
||||
@staticmethod
|
||||
def create_session(database_config: dict) -> tuple[bool, sqlalchemy.engine.Engine | None, sqlalchemy.orm.Session | None]:
|
||||
ok, connection_string = checks.generate_connection_string(database_config)
|
||||
if not ok:
|
||||
return False, None, None
|
||||
|
||||
log.info(connection_string)
|
||||
|
||||
try:
|
||||
engine = sqlalchemy.create_engine(connection_string, echo=False)
|
||||
|
||||
local_session = sqlalchemy.orm.sessionmaker(bind=engine)
|
||||
session = local_session()
|
||||
|
||||
return True, engine, session
|
||||
|
||||
except Exception as err:
|
||||
log.critical(f'database initialization failed: {err}')
|
||||
return False, None, None
|
||||
|
||||
@staticmethod
|
||||
def verify_contents(engine) -> bool:
|
||||
try:
|
||||
log.info('checking if all tables are present, and creating them if they are not')
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
return True
|
||||
|
||||
except Exception as err:
|
||||
log.error('failed to verify contents of the database')
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user