Spaces:
Sleeping
Sleeping
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import sessionmaker | |
| from sqlalchemy.ext.declarative import declarative_base | |
| from app.config import settings | |
| # Use NEON_DATABASE_URL if available, otherwise fall back to DATABASE_URL or SQLite | |
| SQLALCHEMY_DATABASE_URL = settings.NEON_DATABASE_URL or settings.DATABASE_URL | |
| # If no database URL is provided or it's a placeholder, use SQLite as fallback | |
| if (not SQLALCHEMY_DATABASE_URL or | |
| SQLALCHEMY_DATABASE_URL.strip() == "" or | |
| "your_" in SQLALCHEMY_DATABASE_URL or | |
| SQLALCHEMY_DATABASE_URL.startswith("postgresql://your") or | |
| SQLALCHEMY_DATABASE_URL.startswith("postgres://your")): | |
| SQLALCHEMY_DATABASE_URL = "sqlite:///./chat_database.db" | |
| print("⚠️ No database URL configured, using SQLite as fallback") | |
| engine = create_engine( | |
| SQLALCHEMY_DATABASE_URL, | |
| connect_args={"check_same_thread": False} if "sqlite" in SQLALCHEMY_DATABASE_URL else {} | |
| ) | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| Base = declarative_base() | |
| def get_db(): | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |