addon
This commit is contained in:
0
backend/app/db/__init__.py
Normal file → Executable file
0
backend/app/db/__init__.py
Normal file → Executable file
8
backend/app/db/base.py
Normal file → Executable file
8
backend/app/db/base.py
Normal file → Executable file
@@ -4,12 +4,20 @@ Linux BenchTools - Database Base
|
||||
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
# Base for main database (benchmarks, devices)
|
||||
Base = declarative_base()
|
||||
|
||||
# Base for peripherals database (separate)
|
||||
BasePeripherals = declarative_base()
|
||||
|
||||
# Import all models here for Alembic/migrations
|
||||
# Main DB models
|
||||
from app.models.device import Device # noqa
|
||||
from app.models.hardware_snapshot import HardwareSnapshot # noqa
|
||||
from app.models.benchmark import Benchmark # noqa
|
||||
from app.models.disk_smart import DiskSMART # noqa
|
||||
from app.models.manufacturer_link import ManufacturerLink # noqa
|
||||
from app.models.document import Document # noqa
|
||||
|
||||
# Peripherals DB models (imported when module enabled)
|
||||
# Will be imported in init_db.py
|
||||
|
||||
48
backend/app/db/init_db.py
Normal file → Executable file
48
backend/app/db/init_db.py
Normal file → Executable file
@@ -3,8 +3,8 @@ Linux BenchTools - Database Initialization
|
||||
"""
|
||||
|
||||
import os
|
||||
from app.db.base import Base
|
||||
from app.db.session import engine
|
||||
from app.db.base import Base, BasePeripherals
|
||||
from app.db.session import engine, engine_peripherals
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
@@ -24,8 +24,48 @@ def init_db():
|
||||
if db_dir:
|
||||
os.makedirs(db_dir, exist_ok=True)
|
||||
|
||||
# Create all tables
|
||||
# Create all tables for main database
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
print(f"✅ Database initialized: {settings.DATABASE_URL}")
|
||||
print(f"✅ Main database initialized: {settings.DATABASE_URL}")
|
||||
print(f"✅ Upload directory created: {settings.UPLOAD_DIR}")
|
||||
|
||||
# Initialize peripherals database if module is enabled
|
||||
if settings.PERIPHERALS_MODULE_ENABLED:
|
||||
init_peripherals_db()
|
||||
|
||||
|
||||
def init_peripherals_db():
|
||||
"""
|
||||
Initialize peripherals database:
|
||||
- Create all tables
|
||||
- Create upload directories
|
||||
- Import peripheral models
|
||||
"""
|
||||
# Import models to register them
|
||||
from app.models.peripheral import (
|
||||
Peripheral, PeripheralPhoto, PeripheralDocument,
|
||||
PeripheralLink, PeripheralLoan
|
||||
)
|
||||
from app.models.location import Location
|
||||
from app.models.peripheral_history import PeripheralLocationHistory
|
||||
|
||||
# Create peripherals upload directories
|
||||
os.makedirs(settings.PERIPHERALS_UPLOAD_DIR, exist_ok=True)
|
||||
os.makedirs(os.path.join(settings.PERIPHERALS_UPLOAD_DIR, "photos"), exist_ok=True)
|
||||
os.makedirs(os.path.join(settings.PERIPHERALS_UPLOAD_DIR, "documents"), exist_ok=True)
|
||||
os.makedirs(os.path.join(settings.PERIPHERALS_UPLOAD_DIR, "locations", "images"), exist_ok=True)
|
||||
os.makedirs(os.path.join(settings.PERIPHERALS_UPLOAD_DIR, "locations", "qrcodes"), exist_ok=True)
|
||||
|
||||
# Create database directory if using SQLite
|
||||
if "sqlite" in settings.PERIPHERALS_DB_URL:
|
||||
db_path = settings.PERIPHERALS_DB_URL.replace("sqlite:///", "")
|
||||
db_dir = os.path.dirname(db_path)
|
||||
if db_dir:
|
||||
os.makedirs(db_dir, exist_ok=True)
|
||||
|
||||
# Create all tables for peripherals database
|
||||
BasePeripherals.metadata.create_all(bind=engine_peripherals)
|
||||
|
||||
print(f"✅ Peripherals database initialized: {settings.PERIPHERALS_DB_URL}")
|
||||
print(f"✅ Peripherals upload directories created: {settings.PERIPHERALS_UPLOAD_DIR}")
|
||||
|
||||
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