Spaces:
Runtime error
Runtime error
| import os | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import sessionmaker | |
| from sqlalchemy.orm import declarative_base | |
| # Use environment variable for production (PostgreSQL on Render) | |
| # Falls back to SQLite for local development | |
| DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./scamdetect.db") | |
| # Render provides postgres:// but SQLAlchemy needs postgresql:// | |
| if DATABASE_URL.startswith("postgres://"): | |
| DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql://", 1) | |
| # check_same_thread is needed only for SQLite | |
| connect_args = {} | |
| if "sqlite" in DATABASE_URL: | |
| connect_args = {"check_same_thread": False} | |
| engine = create_engine(DATABASE_URL, connect_args=connect_args) | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| Base = declarative_base() | |
| # Dependency for API endpoints to get the DB session | |
| def get_db(): | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |