2026-04-06 12:41:49 +01:00
|
|
|
"""
|
|
|
|
|
Database connections and init
|
|
|
|
|
|
|
|
|
|
Exports:
|
|
|
|
|
- db_dependency
|
|
|
|
|
- Base (sqlalchemy base model)
|
|
|
|
|
"""
|
|
|
|
|
from typing import Annotated
|
|
|
|
|
from sqlalchemy import create_engine
|
|
|
|
|
from sqlalchemy.orm import declarative_base, sessionmaker, Session
|
|
|
|
|
|
|
|
|
|
from fastapi import Depends
|
|
|
|
|
|
|
|
|
|
from src.config import SQLALCHEMY_DATABASE_URI
|
|
|
|
|
|
|
|
|
|
engine = create_engine(SQLALCHEMY_DATABASE_URI.get_secret_value())
|
|
|
|
|
|
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
2026-05-21 16:55:15 +01:00
|
|
|
|
2026-04-06 12:41:49 +01:00
|
|
|
def get_db():
|
2026-05-21 16:55:15 +01:00
|
|
|
db = SessionLocal()
|
|
|
|
|
try:
|
|
|
|
|
yield db
|
|
|
|
|
except:
|
|
|
|
|
db.rollback()
|
|
|
|
|
raise
|
|
|
|
|
finally:
|
|
|
|
|
db.close()
|
2026-04-06 12:41:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
db_dependency = Annotated[Session, Depends(get_db)]
|
|
|
|
|
Base = declarative_base()
|