import os from pathlib import Path from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, declarative_base BASE_DIR = Path(__file__).resolve().parent # Use /home/appuser/data on HF Spaces (writable), fall back to local data/ dir _home_data = Path.home() / "data" _local_data = BASE_DIR.parent / "data" DATA_DIR = _home_data if os.environ.get("SPACE_ID") else _local_data DATA_DIR.mkdir(parents=True, exist_ok=True) DB_PATH = DATA_DIR / "satellite_app.db" DATABASE_URL = os.environ.get("DATABASE_URL", f"sqlite:///{DB_PATH}") # Render gives postgres:// but SQLAlchemy 2.x requires postgresql:// if DATABASE_URL.startswith("postgres://"): DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql://", 1) connect_args = {"check_same_thread": False} if DATABASE_URL.startswith("sqlite") else {} engine = create_engine(DATABASE_URL, connect_args=connect_args) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() def get_db(): db = SessionLocal() try: yield db finally: db.close()