Files
beige/backend/app/core/database.py
yangqianqian 6a2632da70 baseline: Clover 独立仓库首次基线提交
将 Clover 从上层产品包旧仓库中独立出来,建立专属版本控制。
当前状态=纵切片端到端已打通(登录→选品→出文出图→审核→下载包),
M1文案质量去套路化已验收。此提交作为后续按核销清单逐条修复的基线。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 11:30:22 +08:00

54 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
app/core/database.py — SQLAlchemy 双库连接MySQL + MongoDB
"""
from motor.motor_asyncio import AsyncIOMotorClient
from sqlalchemy import create_engine
from sqlalchemy.orm import DeclarativeBase, sessionmaker
from app.core.config import get_settings
settings = get_settings()
# ── MySQL主业务库──────────────────────────────────
engine = create_engine(
settings.DATABASE_URL,
pool_pre_ping=True,
pool_size=10,
max_overflow=20,
echo=(settings.APP_ENV == "development"),
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
class Base(DeclarativeBase):
"""所有 ORM 模型的基类。"""
pass
def get_db():
"""FastAPI 依赖注入:获取 DB Session用完自动关闭。"""
db = SessionLocal()
try:
yield db
finally:
db.close()
# ── MongoDB只存 AI debug trace业务不依赖──────────
_mongo_client: AsyncIOMotorClient | None = None
def get_mongo_client() -> AsyncIOMotorClient:
global _mongo_client
if _mongo_client is None:
_mongo_client = AsyncIOMotorClient(settings.MONGO_URI)
return _mongo_client
def get_mongo_db():
"""获取 MongoDB 数据库实例clover_trace"""
client = get_mongo_client()
return client["clover_trace"]