from __future__ import annotations from os import getenv from pathlib import Path from sqlalchemy import create_engine from sqlalchemy.orm import declarative_base, sessionmaker DATA_DIR = Path(__file__).resolve().parent.parent.parent / "data" # stockage SQLite + raw JSON dans backend/data DATA_DIR.mkdir(parents=True, exist_ok=True) DEFAULT_DATABASE_PATH = DATA_DIR / "suivi.db" DEFAULT_DATABASE_URL = f"sqlite:///{DEFAULT_DATABASE_PATH}" DATABASE_URL = getenv("DATABASE_URL", DEFAULT_DATABASE_URL) if DATABASE_URL.startswith("sqlite:///"): sqlite_path = Path(DATABASE_URL.replace("sqlite:///", "", 1)) sqlite_path.parent.mkdir(parents=True, exist_ok=True) else: DEFAULT_DATABASE_PATH.parent.mkdir(parents=True, exist_ok=True) engine = create_engine( DATABASE_URL, connect_args={"check_same_thread": False}, future=True, ) SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False, future=True) # classe de base pour les modèles SQLAlchemy Base = declarative_base()