fix: drop legacy documents_fts table if exists#7706
fix: drop legacy documents_fts table if exists#7706Soulter merged 2 commits intoAstrBotDevs:masterfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the FTS5 initialization in document_storage.py by adding helper methods for table creation and inspection, including logic to recreate incompatible legacy tables. A new unit test verifies recovery from non-FTS tables. A suggestion was made to improve the robustness of the table inspection logic by normalizing whitespace when checking for the contentless_delete parameter.
这个是用户自行创建的表? |
不是说一定是用户手动创建的 |
不过这个问题是在livingmemory场景下暴露出来的,但我目前不能严格保证那个普通表就是livingmemory创建的 |
* fix: recover FTS5 index from legacy documents_fts table * fix: normalize SQL whitespace when checking contentless_delete
概要
这个 PR 修复了 AstrBot
v4.23.2引入的一个 SQLite FTS5 兼容性问题。当用户本地数据库中已经存在一个普通表
documents_fts时,执行:SQLite 不会报错,但也不会把这个普通表替换成 FTS5 虚表。随后 AstrBot 会误认为 FTS5 已可用,并在后续写入时触发错误:
问题根因
DocumentStorage._initialize_fts5()之前只尝试创建 FTS 表,但没有校验当前已有的documents_fts是否真的是一个合法的 FTS5 虚表,也没有确认它是否包含预期的search_text字段。这会导致以下问题场景:
v4.23.2documents_ftsCREATE VIRTUAL TABLE IF NOT EXISTS ...时被 SQLite 静默跳过fts5_available标记为Truesearch_text时直接报错变更内容
本 PR 做了以下修复:
documents_fts是否为合法的 FTS5 虚表search_textcontentless_delete是否可用,而不是仅根据创建路径推断测试
已验证以下检查通过:
python -m ruff check astrbot/core/db/vec_db/faiss_impl/document_storage.py tests/unit/test_document_storage_fts.pypython -m pytest tests/unit/test_document_storage_fts.py tests/unit/test_sparse_retriever.py另外,新增的回归测试覆盖了以下场景:
documents_ftsDocumentStorage说明
这个问题是在下游插件用户升级到 AstrBot
v4.23.2后暴露出来的。根本原因不在插件本身,而在 AstrBot 核心对旧 SQLite 状态的兼容处理不完整。这个修复的目标是让 AstrBot 在面对历史遗留数据库结构时,能够自动识别并恢复到正确的 FTS5 状态,避免用户在升级后因本地数据库残留结构而直接报错。