satdetect / app /database.py
coderuday21's picture
HF fix: uvicorn-only CMD, defensive mkdir, remove unused numpy, overlay mkdir on save, README troubleshooting
717d61b
raw
history blame
1.21 kB
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
try:
DATA_DIR.mkdir(parents=True, exist_ok=True)
except Exception:
pass # avoid crashing if read-only or permission issue (e.g. some HF environments)
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()