Spaces:
Sleeping
Sleeping
File size: 1,139 Bytes
600f3a7 | 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 | 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()
|