fix SQLAlchemy compatibility issues on macOS#7724
Open
hibiki233i wants to merge 5 commits intoAstrBotDevs:masterfrom
Open
fix SQLAlchemy compatibility issues on macOS#7724hibiki233i wants to merge 5 commits intoAstrBotDevs:masterfrom
hibiki233i wants to merge 5 commits intoAstrBotDevs:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The raw SQL in
_ensure_documents_tablehardcodes thedocumentsschema independently ofBaseDocModel, which risks divergence if the model changes later; consider centralizing the schema definition or at least adding a clear comment tying this DDL to the model so future changes stay in sync. - In
_ensure_documents_table(and the related FTS helpers), theexecutorargument is actually an async connection; renaming it (e.g., toconn) and adding a concrete type hint (such asAsyncConnection) would make its expected interface clearer and reduce misuse.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The raw SQL in `_ensure_documents_table` hardcodes the `documents` schema independently of `BaseDocModel`, which risks divergence if the model changes later; consider centralizing the schema definition or at least adding a clear comment tying this DDL to the model so future changes stay in sync.
- In `_ensure_documents_table` (and the related FTS helpers), the `executor` argument is actually an async connection; renaming it (e.g., to `conn`) and adding a concrete type hint (such as `AsyncConnection`) would make its expected interface clearer and reduce misuse.
## Individual Comments
### Comment 1
<location path="astrbot/core/knowledge_base/kb_db_sqlite.py" line_range="43-47" />
<code_context>
self.DATABASE_URL,
echo=False,
future=True,
+ poolclass=NullPool,
)
self.async_session_maker = sessionmaker(
</code_context>
<issue_to_address>
**suggestion:** Remove or adjust `pool_pre_ping`/`pool_recycle` when using `NullPool` for the SQLite engine.
With `poolclass=NullPool`, connections are created and disposed per use, so `pool_pre_ping` and `pool_recycle` are effectively no-ops and may mislead future readers about connection liveness guarantees. Consider removing these arguments here, or switching to an actual pool (e.g., `StaticPool`/`SingletonThreadPool`) if those behaviors are required for this database.
```suggestion
echo=False,
poolclass=NullPool,
)
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Contributor
There was a problem hiding this comment.
Code Review
This pull request improves stability for packaged desktop runtimes by switching to NullPool for SQLite connections and using raw SQL for table creation in the vector database. It also prevents library mixing in main.py for packaged environments. Feedback includes a suggestion to add a UNIQUE constraint to the doc_id column to prevent potential query errors and to remove redundant pooling configurations that are no longer effective with NullPool.
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Member
|
@sourcery-ai review |
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The new
_ensure_documents_tableuses a hardcodedCREATE TABLEschema; consider centralizing the table definition (or deriving it fromBaseDocModel) so that field/constraint changes in the model cannot silently drift from the raw SQL. - The
is_sqlite = "sqlite" in self.DATABASE_URLcheck inastrbot/core/db/__init__.pycould mis-detect non-SQLite URLs containing the substring; using SQLAlchemy’s URL parsing (make_url) or a stricter prefix/scheme check would make the engine configuration more robust.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `_ensure_documents_table` uses a hardcoded `CREATE TABLE` schema; consider centralizing the table definition (or deriving it from `BaseDocModel`) so that field/constraint changes in the model cannot silently drift from the raw SQL.
- The `is_sqlite = "sqlite" in self.DATABASE_URL` check in `astrbot/core/db/__init__.py` could mis-detect non-SQLite URLs containing the substring; using SQLAlchemy’s URL parsing (`make_url`) or a stricter prefix/scheme check would make the engine configuration more robust.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix #7722
Modifications / 改动点
packaged desktop runtime 把 app 内置依赖和 ~/.astrbot/data/site-packages 里的用户依赖混到了同一个 Python 进程里。
main.py 调整了启动时的 sys.path 处理逻辑。现在在 packaged desktop runtime 下,不再把 ~/.astrbot/data/site-packages 直接追加进全局导入路径。核心后端优先固定使用 app 自带依赖,避免和用户目录里的 SQLAlchemy、SQLModel 等包混装。
astrbot/core/db/init.py 、astrbot/core/knowledge_base/kb_db_sqlite.py 、astrbot/core/db/vec_db/faiss_impl/document_storage.py 把 sqlite+aiosqlite 的异步引擎都改成了显式 NullPool。不再依赖 SQLAlchemy 对 SQLite async 的默认连接池选择,减少 desktop/launcher 打包环境里因为默认池事件支持差异导致的兼容性问题。
documents 表初始化从 metadata.create_all() 改成了显式 SQL CREATE TABLE IF NOT EXISTS。知识库向量库文档表的建表不再依赖 ORM metadata 反射路径,从而绕开 packaged runtime 下最容易被双份 SQLAlchemy 污染的 has_table()/create_all() 调用链。
Screenshots or Test Results / 运行截图或测试结果
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
[√] 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
[√] 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。[√] 😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Ensure packaged desktop runtime uses bundled dependencies reliably and avoids SQLite async engine issues in bundled environments.
Bug Fixes:
Enhancements:
Summary by Sourcery
Resolve packaged desktop runtime compatibility issues with SQLAlchemy and SQLite on macOS by isolating bundled dependencies and hardening SQLite async usage.
Bug Fixes:
Enhancements: