File size: 1,095 Bytes
401aaf2
 
 
 
 
 
 
60f20a4
 
 
 
 
 
 
 
401aaf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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()