Skip to content

refactor(db)!: replace SQLite Driver with glebarez/sqlite to avoid CGO#2211

Open
PIKACHUIM wants to merge 4 commits intomainfrom
dev-sqlite
Open

refactor(db)!: replace SQLite Driver with glebarez/sqlite to avoid CGO#2211
PIKACHUIM wants to merge 4 commits intomainfrom
dev-sqlite

Conversation

@PIKACHUIM
Copy link
Member

@PIKACHUIM PIKACHUIM commented Mar 9, 2026

Description / 描述

将 SQLite 数据库驱动从基于 CGO 的 gorm.io/driver/sqlite(底层依赖 mattn/go-sqlite3)替换为纯 Go 实现的 github.com/glebarez/sqlite(底层依赖 modernc.org/sqlite),彻底消除对 CGO 的依赖,实现真正的纯 Go 跨平台编译。

主要变更:

  • internal/bootstrap/db.go:将 SQLite 驱动导入从 gorm.io/driver/sqlite 替换为 github.com/glebarez/sqlite
  • go.mod:新增 github.com/glebarez/sqlite v1.11.0,移除 github.com/mattn/go-sqlite3(CGO 依赖),同步升级 gorm.io/gorm v1.25.11 → v1.30.0
  • go.sum:更新依赖哈希

Motivation and Context / 背景

原有的 gorm.io/driver/sqlite 驱动底层依赖 mattn/go-sqlite3,该库需要通过 CGO 编译 C 代码,导致:

  1. 交叉编译困难:CGO 交叉编译需要配置对应目标平台的 C 编译器工具链,增加了构建复杂度
  2. 构建环境要求高:CI/CD 环境需要安装 GCC 等 C 编译器
  3. 纯 Go 构建不可用:无法使用 CGO_ENABLED=0 进行纯 Go 静态编译

github.com/glebarez/sqlitegorm.io/driver/sqlite 的纯 Go 替代品,底层使用 modernc.org/sqlite(SQLite 官方 C 代码通过工具自动转译为 Go 代码),与 GORM 完全兼容,无需任何 CGO 即可完成编译。

How Has This Been Tested? / 测试

  • 使用 CGO_ENABLED=0 go build 验证纯 Go 编译成功
  • 启动服务,使用 SQLite 数据库类型(sqlite3)验证数据库连接正常
  • 验证开发模式(--dev)下内存数据库(file::memory:?cache=shared)正常工作
  • 验证 WAL 模式(_journal=WAL&_vacuum=incremental)下数据库读写正常
  • 验证 MySQL、PostgreSQL 数据库类型不受影响

Checklist / 检查清单

  • I have read the CONTRIBUTING document.
    我已阅读 CONTRIBUTING 文档。
  • I have formatted my code with go fmt or prettier.
    我已使用 go fmtprettier 格式化提交的代码。
  • I have added appropriate labels to this PR (or mentioned needed labels in the description if lacking permissions).
    我已为此 PR 添加了适当的标签(建议添加 enhancementdependencies 标签)。
  • I have requested review from relevant code authors using the "Request review" feature when applicable.
    我已在适当情况下使用"Request review"功能请求相关代码作者进行审查。
  • I have updated the repository accordingly (If it's needed).
    我已相应更新了相关仓库(若适用)。
    • OpenList-Frontend #XXXX(此 PR 为纯后端依赖替换,无需更新前端)
    • OpenList-Docs #XXXX(此 PR 为纯后端依赖替换,无需更新文档)

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR switches the project’s SQLite GORM dialect from the CGO-based gorm.io/driver/sqlite (mattn/go-sqlite3) to the pure-Go github.com/glebarez/sqlite (modernc.org/sqlite) to enable CGO_ENABLED=0 builds.

Changes:

  • Updated DB bootstrap code to open SQLite connections via github.com/glebarez/sqlite.
  • Updated Go module dependencies, including adding github.com/glebarez/sqlite and bumping gorm.io/gorm to v1.30.0.
  • Refreshed dependency hashes in go.sum.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 3 comments.

File Description
internal/bootstrap/db.go Switches the SQLite dialect import/usage to github.com/glebarez/sqlite.
go.mod Adds github.com/glebarez/sqlite, upgrades GORM, and adjusts indirect deps (but still retains CGO SQLite deps).
go.sum Updates module sums to reflect the dependency graph changes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@PIKACHUIM
Copy link
Member Author

@copilot open a new pull request to apply changes based on the comments in this thread

Copy link
Contributor

Copilot AI commented Mar 9, 2026

@PIKACHUIM I've opened a new pull request, #2213, to work on those changes. Once the pull request is ready, I'll request review from you.

* Initial plan

* fix: address review comments - fix import order, update test, remove CGO sqlite deps

Co-authored-by: PIKACHUIM <40362270+PIKACHUIM@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: PIKACHUIM <40362270+PIKACHUIM@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 4 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants