feat: add database configuration and health check
This commit is contained in:
parent
3d53a94d59
commit
3701c50711
9 changed files with 72 additions and 2 deletions
BIN
backend/hermes/app/db/__pycache__/database.cpython-314.pyc
Normal file
BIN
backend/hermes/app/db/__pycache__/database.cpython-314.pyc
Normal file
Binary file not shown.
BIN
backend/hermes/app/db/__pycache__/health.cpython-314.pyc
Normal file
BIN
backend/hermes/app/db/__pycache__/health.cpython-314.pyc
Normal file
Binary file not shown.
29
backend/hermes/app/db/database.py
Normal file
29
backend/hermes/app/db/database.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
engine = create_engine(
|
||||
settings.database_url,
|
||||
echo=False
|
||||
)
|
||||
|
||||
SessionLocal = sessionmaker(
|
||||
autocommit=False,
|
||||
autoflush=False,
|
||||
bind=engine
|
||||
)
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
12
backend/hermes/app/db/health.py
Normal file
12
backend/hermes/app/db/health.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
from sqlalchemy import text
|
||||
|
||||
from app.db.database import engine
|
||||
|
||||
|
||||
def check_database() -> bool:
|
||||
try:
|
||||
with engine.connect() as connection:
|
||||
connection.execute(text("SELECT 1"))
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
Loading…
Add table
Add a link
Reference in a new issue