import os from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, declarative_base # In production, set DATABASE_URL env var to a Postgres URL. SQLALCHEMY_DATABASE_URL = os.getenv( "DATABASE_URL", "sqlite:///./emotion_system.db" ) # Cloud providers sometimes give 'postgres://', but SQLAlchemy requires 'postgresql://' if SQLALCHEMY_DATABASE_URL.startswith("postgres://"): SQLALCHEMY_DATABASE_URL = SQLALCHEMY_DATABASE_URL.replace("postgres://", "postgresql://", 1) connect_args = {"check_same_thread": False} if SQLALCHEMY_DATABASE_URL.startswith("sqlite") else {} engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args=connect_args) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base()