Files
company-ai-platform/app/core/database.py
JiuContinent e3a4a6d426 ```
feat: 添加审批系统和遗留查询功能支持

- 添加审批系统,包括审批请求模型、服务和路由,支持创建、批准和拒绝操作
- 实现审批API密钥验证机制,区分普通API和审批API访问权限
- 添加Alembic数据库迁移支持,更新初始schema版本并添加降级保护
- 配置遗留MySQL查询白名单机制,支持命名查询和参数化查询
- 更新业务服务以集成审批流程,高风险操作需要审批票证
- 调整安全认证使用常量定义的HTTP头,增强安全性比较
- 优化.gitignore配置,添加日志目录排除和文档文件包含规则
- 更新Dockerfile添加alembic依赖包,修复OpenClaw适配器错误处理
```
2026-07-06 09:39:25 +08:00

47 lines
1.2 KiB
Python

from collections.abc import Generator
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from app.core.config import get_settings
from app.core.db_base import Base
settings = get_settings()
engine = create_engine(settings.database_url, pool_pre_ping=True, pool_recycle=3600)
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False, expire_on_commit=False)
legacy_engine = (
create_engine(settings.legacy_database_url, pool_pre_ping=True, pool_recycle=3600)
if settings.legacy_database_url
else None
)
LegacySessionLocal = (
sessionmaker(bind=legacy_engine, autoflush=False, autocommit=False, expire_on_commit=False)
if legacy_engine
else None
)
def get_db() -> Generator[Session, None, None]:
"""Yield an application database session for FastAPI dependencies."""
db = SessionLocal()
try:
yield db
finally:
db.close()
def get_legacy_db() -> Generator[Session, None, None]:
"""Yield a legacy database session when the legacy connection is configured."""
if LegacySessionLocal is None:
raise RuntimeError("LEGACY_DATABASE_URL is not configured")
db = LegacySessionLocal()
try:
yield db
finally:
db.close()