addon
This commit is contained in:
62
backend/app/db/session.py
Normal file → Executable file
62
backend/app/db/session.py
Normal file → Executable file
@@ -1,28 +1,70 @@
|
||||
"""
|
||||
Linux BenchTools - Database Session
|
||||
Linux BenchTools - Database Sessions
|
||||
"""
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.orm import sessionmaker, Session
|
||||
from app.core.config import settings
|
||||
|
||||
# Create engine
|
||||
engine = create_engine(
|
||||
|
||||
# ========================================
|
||||
# DATABASE PRINCIPALE (Benchmarks)
|
||||
# ========================================
|
||||
|
||||
# Create main engine
|
||||
engine_main = create_engine(
|
||||
settings.DATABASE_URL,
|
||||
connect_args={"check_same_thread": False} if "sqlite" in settings.DATABASE_URL else {},
|
||||
echo=False, # Set to True for SQL query logging during development
|
||||
)
|
||||
|
||||
# Create SessionLocal class
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
# Create SessionLocal class for main DB
|
||||
SessionLocalMain = sessionmaker(autocommit=False, autoflush=False, bind=engine_main)
|
||||
|
||||
# Backward compatibility
|
||||
engine = engine_main
|
||||
SessionLocal = SessionLocalMain
|
||||
|
||||
|
||||
# Dependency to get DB session
|
||||
def get_db():
|
||||
# ========================================
|
||||
# DATABASE PÉRIPHÉRIQUES
|
||||
# ========================================
|
||||
|
||||
# Create peripherals engine
|
||||
engine_peripherals = create_engine(
|
||||
settings.PERIPHERALS_DB_URL,
|
||||
connect_args={"check_same_thread": False} if "sqlite" in settings.PERIPHERALS_DB_URL else {},
|
||||
echo=False,
|
||||
)
|
||||
|
||||
# Create SessionLocal class for peripherals DB
|
||||
SessionLocalPeripherals = sessionmaker(
|
||||
autocommit=False,
|
||||
autoflush=False,
|
||||
bind=engine_peripherals
|
||||
)
|
||||
|
||||
|
||||
# ========================================
|
||||
# DEPENDENCY INJECTION
|
||||
# ========================================
|
||||
|
||||
def get_db() -> Session:
|
||||
"""
|
||||
Database session dependency for FastAPI
|
||||
Main database session dependency for FastAPI (benchmarks, devices)
|
||||
"""
|
||||
db = SessionLocal()
|
||||
db = SessionLocalMain()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def get_peripherals_db() -> Session:
|
||||
"""
|
||||
Peripherals database session dependency for FastAPI
|
||||
"""
|
||||
db = SessionLocalPeripherals()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user