From a8598e755d92c1418772235442e3df030a37d68a Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Sat, 25 Jul 2026 23:39:08 +0800 Subject: [PATCH] fix: migrate memory domain before indexing --- core/store.py | 5 +++-- docs/运维记忆错题本.md | 10 ++++++++++ tests/test_user_memory_contract.py | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/core/store.py b/core/store.py index c51636e..3e0ce99 100644 --- a/core/store.py +++ b/core/store.py @@ -236,8 +236,6 @@ class MemoryStore: supersedes_id TEXT NOT NULL DEFAULT '' ); - CREATE INDEX IF NOT EXISTS idx_memories_domain ON memories(memory_domain, lifecycle, scope, group_id, subject_id); - CREATE INDEX IF NOT EXISTS idx_memories_scope_session ON memories(scope, session_id, group_id, subject_id, object_id); CREATE INDEX IF NOT EXISTS idx_memories_visibility @@ -491,6 +489,9 @@ class MemoryStore: """ ) self._ensure_memory_columns_sync() + self._conn.execute( + "CREATE INDEX IF NOT EXISTS idx_memories_domain ON memories(memory_domain, lifecycle, scope, group_id, subject_id)" + ) self._ensure_timeline_columns_sync() self._ensure_acl_columns_sync() self._ensure_memory_fts_sync() diff --git a/docs/运维记忆错题本.md b/docs/运维记忆错题本.md index ec82760..7079a95 100644 --- a/docs/运维记忆错题本.md +++ b/docs/运维记忆错题本.md @@ -105,3 +105,13 @@ - 修复或规避:不修改普通陪伴分支;将宿主依赖测试单独标记为环境阻塞,其余 142 项运维版测试正常执行。 - 预防规则:跨插件测试必须区分纯单元测试与宿主集成测试;宿主依赖缺失不能被误报为业务回归。 - 验证证据:`compileall -q core tests page_api.py` 通过;`git diff --check` 通过;排除 `test_security_cache` 后 `142 tests` 全部通过;定向 Q5、Q4.2、总结、检索和存储测试全部通过。 + +## 2026-07-25 — Q5 首次 VM 部署暴露旧库索引迁移顺序 + +- 阶段:Q5 VM 部署验收与修复 +- 类型:部署 / 数据库迁移 / API契约 +- 现象:提交 `66b5d0b` 部署后 AstrBot 服务仍 active,但运维记忆插件初始化失败,日志报 `sqlite3.OperationalError: no such column: memory_domain`,Archivist 自动降级。 +- 根因:`memories` 旧表在字段迁移前执行了依赖新字段的 `idx_memories_domain` 创建。 +- 修复或规避:将域索引创建移动到 `_ensure_memory_columns_sync()` 之后;增加真实 Q4 schema 去掉单字段的升级测试;不删除或重建用户数据目录。 +- 预防规则:任何新增数据库列必须先完成幂等字段迁移,再创建索引、触发器和查询约束;部署验收必须看插件加载日志和 Bridge 可用性,不能只看 systemd active。 +- 验证证据:本地旧库迁移测试通过;待修复提交重新部署后复核插件加载、Archivist capability、服务状态和数据目录。 diff --git a/tests/test_user_memory_contract.py b/tests/test_user_memory_contract.py index e34b722..cb98f71 100644 --- a/tests/test_user_memory_contract.py +++ b/tests/test_user_memory_contract.py @@ -82,6 +82,29 @@ class UserMemoryContractTests(unittest.IsolatedAsyncioTestCase): with self.assertRaisesRegex(UserMemoryBoundaryError, "missing_user_evidence"): validate_user_memory_record(record) + async def test_existing_q4_database_adds_domain_before_domain_index(self) -> None: + with tempfile.TemporaryDirectory() as temp: + seed_path = Path(temp) / "seed.db" + legacy_path = Path(temp) / "legacy.db" + seed = MemoryStore(seed_path) + seed.initialize() + schema = seed._conn.execute( + "SELECT sql FROM sqlite_master WHERE type='table' AND name='memories'" + ).fetchone()[0] + seed.close() + legacy_schema = schema.replace(",\n memory_domain TEXT NOT NULL DEFAULT 'unknown'", "") + connection = __import__("sqlite3").connect(legacy_path) + connection.execute(legacy_schema) + connection.commit() + connection.close() + store = MemoryStore(legacy_path) + store.initialize() + columns = {row["name"] for row in store._conn.execute("PRAGMA table_info(memories)").fetchall()} + indexes = {row["name"] for row in store._conn.execute("PRAGMA index_list(memories)").fetchall()} + self.assertIn("memory_domain", columns) + self.assertIn("idx_memories_domain", indexes) + store.close() + async def test_store_persists_time_domain_and_deduplicates_explicit_id(self) -> None: with tempfile.TemporaryDirectory() as temp: store = MemoryStore(Path(temp) / "memory.db")