Spaces:
Runtime error
Runtime error
File size: 1,575 Bytes
407bf6d | 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 36 37 38 39 40 41 42 43 44 45 | import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from dotenv import load_dotenv
# Load env before importing models
load_dotenv()
from models.base import Base
# Import all models here so metadata is aware of them
from models.chat import Conversation, Message, Attachment
from models.user import User
# Use DATABASE_URL from .env, fallback to SQLite for local dev if missing
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./aura_database.db")
DATABASE_URL = DATABASE_URL.strip('"').strip("'")
# OVERRIDE: If the hosting environment (like Hugging Face Spaces) is injecting the dummy URL, replace it
if "db.mfmxknljzzpddwclqxlx.supabase.co" in DATABASE_URL:
DATABASE_URL = "postgresql://postgres.mfmxknljzzpddwclqxlx:Aura_llm_india@aws-1-ap-northeast-1.pooler.supabase.com:6543/postgres"
# If using PostgreSQL, we might need different connect_args than SQLite
connect_args = {}
if DATABASE_URL.startswith("sqlite"):
connect_args["check_same_thread"] = False
# Supabase fix: sometimes requires sslmode=require but Supabase connection strings usually have it.
if DATABASE_URL.startswith("postgres://"):
DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql://", 1)
engine = create_engine(
DATABASE_URL, connect_args=connect_args
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def init_db():
Base.metadata.create_all(bind=engine)
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
|