- 重构报销状态注册表、审批流路由与平台风险标记 - 完善管家意图规划器与模型计划构建器全链路 - 新增 OCR Worker 脚本、数据库会话管理与通知状态 - 优化文档中心、日志视图、预算中心与员工管理交互 - 增强工作台摘要、图标资源与全局主题样式 - 补充审批路由、状态注册、OCR 服务与管家规划器测试覆盖
54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.engine import Engine
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
|
|
from app.core.config import get_settings
|
|
|
|
_engine: Engine | None = None
|
|
_session_factory: sessionmaker[Session] | None = None
|
|
|
|
|
|
def configure_session_factory() -> None:
|
|
global _engine, _session_factory
|
|
|
|
settings = get_settings()
|
|
|
|
if _engine is not None:
|
|
_engine.dispose()
|
|
|
|
engine_kwargs = {
|
|
"echo": settings.sqlalchemy_echo,
|
|
"pool_pre_ping": True,
|
|
}
|
|
if not settings.resolved_database_url.startswith("sqlite"):
|
|
engine_kwargs.update(
|
|
{
|
|
"pool_size": max(1, int(settings.sqlalchemy_pool_size or 10)),
|
|
"max_overflow": max(0, int(settings.sqlalchemy_max_overflow or 20)),
|
|
"pool_timeout": max(1, int(settings.sqlalchemy_pool_timeout or 30)),
|
|
}
|
|
)
|
|
|
|
_engine = create_engine(settings.resolved_database_url, **engine_kwargs)
|
|
_session_factory = sessionmaker(bind=_engine, autoflush=False, autocommit=False)
|
|
|
|
|
|
def get_engine() -> Engine:
|
|
global _engine
|
|
|
|
if _engine is None:
|
|
configure_session_factory()
|
|
|
|
return _engine
|
|
|
|
|
|
def get_session_factory() -> sessionmaker[Session]:
|
|
global _session_factory
|
|
|
|
if _session_factory is None:
|
|
configure_session_factory()
|
|
|
|
return _session_factory
|