refactor(backend): 后端重构初步设想

This commit is contained in:
sophie committed 2026-06-17 11:24:54 +08:00
1 parent b69d549b2c
commit 21b8c24815
96 files changed
+1592

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
"""
backend/app/core/db/__init__.py
用途:数据库会话工具包入口。
逻辑:Wave 1 提供 run_with_retry;sequence 工具供迁移/运维脚本使用。
"""
+7
View File
@@ -0,0 +1,7 @@
"""
backend/app/core/db/sequence.py
用途:PostgreSQL 序列修复工具(Wave 1 自 services/db_sequence.py 迁入)。
逻辑:迁移脚本与 check_schema_drift 调用。
"""
from app.services.db_sequence import * # noqa: F403
+28
View File
@@ -0,0 +1,28 @@
"""
backend/app/core/db/session.py
用途:数据库会话辅助(重试、只读查询封装)。
逻辑:Wave 1 从 api/user.py、knowledge_base.py 等抽取 _run_db_with_retry 模式。
"""
from __future__ import annotations
from collections.abc import Callable
from typing import TypeVar
T = TypeVar('T')
def run_with_retry(fn: Callable[[], T], *, max_attempts: int = 3) -> T:
"""
在数据库短暂失败时重试可调用对象。
核心逻辑:捕获 OperationalError/DisconnectionError 后 rollback 并重试。
Args:
fn: 无参 callable,内部使用 db.session。
max_attempts: 最大尝试次数。
Returns:
fn 的返回值。
Raises:
最后一次失败的异常。
"""
# [STEP 1] Wave 0 占位:Wave 1 从 user.py / knowledge_base.py 抽取实现
return fn()