fix: add close/context-manager to diagnosis store; tighten import boundary

This commit is contained in:
2026-07-15 12:06:29 -04:00
parent 6b85fcbe1c
commit 3a8dd5c167
3 changed files with 44 additions and 5 deletions
+19
View File
@@ -10,9 +10,13 @@ from __future__ import annotations
import sqlite3
from pathlib import Path
from typing import TYPE_CHECKING
from core.evolution.types import DiagnosisSignalRow
if TYPE_CHECKING:
from types import TracebackType
# 表列顺序即 DiagnosisSignalRow 字段顺序(question_id..session_id),
# upsert 写入与 load 还原共用,避免手写列名两处漂移。
_COLUMNS: tuple[str, ...] = (
@@ -152,3 +156,18 @@ class SqliteDiagnosisSignalStore:
)
for r in cursor.fetchall()
]
def close(self) -> None:
"""关闭底层 SQLite 连接,释放文件描述符与锁。"""
self._conn.close()
def __enter__(self) -> SqliteDiagnosisSignalStore:
return self
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
self.close()