Skip to content

fix(db): 新增迁移删除废弃的 userAgent 列,修复升级即崩 - #26

Closed
aaazhouaa wants to merge 1 commit into
jieapi:mainfrom
aaazhouaa:hotfix/db-useragent-migration
Closed

aaazhouaa wants to merge 1 commit into
jieapi:mainfrom
aaazhouaa:hotfix/db-useragent-migration

Conversation

@aaazhouaa

@aaazhouaa aaazhouaa commented Sep 18, 2026

Copy link
Copy Markdown

问题

从任意旧版本升级到本版本的用户,App 启动即崩:

java.lang.IllegalStateException: Migration didn't properly handle:
  ai_providers(com.aicode.feature.settings.data.local.entity.AIProviderEntity).
Expected: ... (无 userAgent 列)
Found:    ... (多出 userAgent 列)
    at ...SQLiteOpenHelper.getDatabaseLocked

根因

提交 04385620「提供商支持自定义请求头与脚本参数」用 customHeaders + scriptParams
取代 userAgent只改了 EntityAIProviderEntity.kt),没有配套迁移删除该列。

userAgent 由迁移 40_add_provider_user_agent.sql 添加,该迁移不带 IF NOT EXISTS
无条件执行
,因此凡 SCHEMA_VERSION < 54 的库在升级时都会把这一列加回来,而新版
Entity 不认识它,Room 表结构校验「表里多出一列」失败,onUpgrade 抛异常。

影响范围

迁移 40 之后的全部已发布版本,即 v1.10.0-rc1 起的任意版本(含 v1.10.0
v1.10.1v1.11.0-rc1 ~ rc10)升级到本版本都会崩。

两点需要说明,避免误判:

  • rc 之间互相升级不受影响。rc 各版本的 Entity 均含 userAgent、彼此一致,
    只有本版本改变了列集,所以问题只在这一跳才暴露。
  • 全新安装不受影响。KSP 生成的建表语句已无该列,因此发布前未发现。

修复

新增迁移 54 重建 ai_providers 表移除 userAgentSCHEMA_VERSION 53 提升到 54。

minSdk 26 对应用户态 SQLite 3.18,无 ALTER TABLE ... DROP COLUMN
故按迁移 25/32 的先例重建表。INSERT ... SELECT 的列清单不含 userAgent
因此对两种源状态都成立(旧库有该列、全新安装的库没有),重复执行也不报
duplicate column。

验证

  • SQLite 端到端复现:从旧版 schema 起套用迁移,复现出 Found 恰好多
    userAgent,与线上崩溃日志逐字段吻合
  • 结构精确对齐:迁移后列名 / 类型 / notNull / 主键 / 索引 / 外键
    均与 Room 期望一致(不只是列名相同)
  • 两条升级路径(有/无 userAgent)收敛到同一列集,业务字段全部保留
  • python3 scripts/check_migrations.py 通过

数据安全

用户的库未损坏,升级后数据不丢。Room 在 onUpgrade 抛异常时回滚了迁移,
user_version 停留在崩溃前的值,未产生半迁移状态。日志佐证:迁移 52 出现 5 次、
53 出现 4 次、51 从不出现,说明每次启动都在从同一位置重跑。

附带建议

check_migrations.py 目前只校验编号连续、SCHEMA_VERSION 一致、已发布迁移未被篡改,
不校验迁移结果与 Entity 是否一致,所以本次迁移对账全绿却依然崩溃。

可考虑增加一项校验:比对「迁移执行后的列集」与「KSP 生成的 TableInfo 期望列集」,
能在 CI 阶段提前拦住这类问题(AgentDatabase_Impl.kt 里已有现成的期望值可用)。

Summary by CodeRabbit

  • Bug Fixes

    • Removed the deprecated provider user-agent field from locally stored AI provider settings.
    • Improved database upgrades so existing data remains available when moving to the latest database version.
  • Chores

    • Updated the local database schema to version 54.

从任何 SCHEMA_VERSION < 54 的旧版本升级到本版本的用户,启动即崩:

  IllegalStateException: Migration didn't properly handle:
  ai_providers(...AIProviderEntity). ...
  Found 比 Expected 多出 userAgent 列

根因:提交 0438562「提供商支持自定义请求头与脚本参数」用
customHeaders + scriptParams 取代 userAgent 时只改了 Entity、
没有写迁移删除列。

userAgent 由迁移 40 添加,该迁移不带 IF NOT EXISTS、无条件执行,
因此凡 SCHEMA_VERSION < 54 的库在升级时都会把这一列加回来,而新版
Entity 不认识它 → Room 表结构校验「表里多出一列」失败 → onUpgrade
抛异常 → App 启动即崩。

影响范围:迁移 40 之后的全部已发布版本(v1.10.0-rc1 起),即
v1.10.0 ~ v1.11.0-rc10 的任意版本升级到本版本都会崩。

注:rc 之间的互相升级不受影响,因为 rc 版本的 Entity 均含 userAgent、
彼此一致,只有本版本改变了列集。全新安装也不受影响(KSP 生成的建表
语句已无该列),所以该问题在发布前未暴露。

修复:新增迁移 54 重建 ai_providers 表移除 userAgent。
minSdk 26 对应用户态 SQLite 3.18,无 ALTER TABLE ... DROP COLUMN,
故按迁移 25/32 的先例重建表。

INSERT ... SELECT 的列清单不含 userAgent,故本迁移对两种源状态都成立
(旧库有该列、全新安装的库没有),重复执行也不报 duplicate column。

已验证:
- SQLite 端到端复现崩溃(多余列恰为 userAgent,与线上崩溃日志一致)
- 迁移后列集/类型/notNull/主键/索引均与 Room 期望精确一致
- 两条升级路径(有/无 userAgent)收敛到同一列集,业务字段全部保留
- scripts/check_migrations.py 通过

用户数据不会丢失:Room 在 onUpgrade 抛异常时回滚了迁移,
user_version 停留在崩溃前的值,未产生半迁移状态。
@vercel

vercel Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

@aaazhouaa is attempting to deploy a commit to the jieapi's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Sep 18, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: cec64fa3-d692-4f38-99dd-1030b6388ee2

📥 Commits

Reviewing files that changed from the base of the PR and between 5bd099e and 74a076d.

📒 Files selected for processing (3)
  • app/schemas/com.aicode.feature.agent.data.local.database.AgentDatabase/54.json
  • app/src/main/assets/migrations/54_drop_provider_user_agent.sql
  • app/src/main/java/com/aicode/feature/agent/data/local/database/AgentDatabase.kt

Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review.


📝 Walkthrough

Walkthrough

AgentDatabase advances from schema version 53 to 54. The exported Room schema records the version 54 entities. Migration 54 rebuilds ai_providers without the deprecated userAgent column while preserving other data.

Changes

AgentDatabase version 54

Layer / File(s) Summary
Version 54 schema export
app/schemas/com.aicode.feature.agent.data.local.database.AgentDatabase/54.json
The Room schema export records version 54, nine entities, their columns, primary keys, indices, foreign keys, and setup queries.
Provider column migration and version activation
app/src/main/assets/migrations/54_drop_provider_user_agent.sql, app/src/main/java/com/aicode/feature/agent/data/local/database/AgentDatabase.kt
Migration 54 rebuilds ai_providers without userAgent, copies the remaining columns, drops the old table, and renames the new table. AgentDatabase.SCHEMA_VERSION changes from 53 to 54.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 74a07

The database upgrade removes only the obsolete column and preserves the current provider data structure, with no actionable migration risk identified.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. (2 skipped: 2 … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the database migration, the removal of the deprecated userAgent column, and the upgrade crash fix. It matches the main changes in the pull request.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. (2 skipped: 2 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@aaazhouaa aaazhouaa closed this Sep 18, 2026
@aaazhouaa
aaazhouaa deleted the hotfix/db-useragent-migration branch September 18, 2026 14:54
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.

1 participant