refactor: make model sessions explicit

This commit is contained in:
jxxghp
2026-08-23 23:33:07 +08:00
parent 820582ab12
commit 6e69258e3c
65 changed files with 1299 additions and 2010 deletions
+6 -6
View File
@@ -1,4 +1,4 @@
"""SQLAlchemy 请求级事务适配器与 Oper 事务执行端口。"""
"""SQLAlchemy 请求级事务适配器与无会话 Oper 事务执行端口。"""
from collections.abc import Awaitable, Callable
from typing import Protocol, TypeVar
@@ -11,7 +11,7 @@ T = TypeVar("T")
class SyncTransactionRunner(Protocol):
"""为无显式 Session 的兼容写入口提供独占同步事务。"""
"""为无显式 Session 的 Oper 入口提供独占同步事务。"""
def __call__(self, operation: Callable[[Session], T]) -> T:
"""在一个独占会话中执行并提交操作。"""
@@ -19,7 +19,7 @@ class SyncTransactionRunner(Protocol):
class AsyncTransactionRunner(Protocol):
"""为无显式 Session 的兼容写入口提供独占异步事务。"""
"""为无显式 Session 的 Oper 入口提供独占异步事务。"""
def __call__(
self,
@@ -38,14 +38,14 @@ def configure_transaction_runners(
sync: SyncTransactionRunner,
async_: AsyncTransactionRunner,
) -> None:
"""由组合根登记 Oper 兼容入口使用的显式事务执行器。"""
"""由组合根登记无会话 Oper 入口使用的显式事务执行器。"""
global _sync_transaction_runner, _async_transaction_runner
_sync_transaction_runner = sync
_async_transaction_runner = async_
def run_sync_transaction(operation: Callable[[Session], T]) -> T:
"""委托组合根在独占同步事务中执行兼容写操作。"""
"""委托组合根在独占同步事务中执行 Oper 操作。"""
if _sync_transaction_runner is None:
raise RuntimeError("同步事务执行器尚未配置")
return _sync_transaction_runner(operation)
@@ -54,7 +54,7 @@ def run_sync_transaction(operation: Callable[[Session], T]) -> T:
async def run_async_transaction(
operation: Callable[[AsyncSession], Awaitable[T]],
) -> T:
"""委托组合根在独占异步事务中执行兼容写操作。"""
"""委托组合根在独占异步事务中执行 Oper 操作。"""
if _async_transaction_runner is None:
raise RuntimeError("异步事务执行器尚未配置")
return await _async_transaction_runner(operation)