Skip to content

fix(db): 兼容历史迁移编号,避免启动时重复添加列 - #14

Closed
MystHiru wants to merge 1 commit into
jieapi:mainfrom
MystHiru:fix/db-migration-compat
Closed

MystHiru wants to merge 1 commit into
jieapi:mainfrom
MystHiru:fix/db-migration-compat

Conversation

@MystHiru

@MystHiru MystHiru commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

问题

部分旧版本数据库曾使用 41/42/43 表示 session_parent、provider_proxy、provider_sort_order。后续版本在 41/42 插入 thinking_blocks 和 cache_creation 后,将原迁移改为 43/44/45。

已升级到旧版本 43 的用户安装新 APK 时,迁移 44 会再次执行 ALTER TABLE ai_providers ADD COLUMN proxyEnabled,触发 duplicate column name: proxyEnabled,应用随后进入 CrashActivity,无法正常启动。

修复

  • 新增 SchemaCatchUp,按实际表结构补齐所有新增列,兼容迁移编号重排。
  • FileMigration 对重复列迁移做幂等处理。
  • 数据库打开时再次执行结构补齐,覆盖旧库已记录迁移但缺少新列的情况。
  • 增加迁移兼容性单元测试。

已在 Android 16 arm64 设备验证:原本启动即崩溃的数据库升级后可进入 MainActivity,数据库升级到版本 45,原有数据保留。

Summary by CodeRabbit

  • Bug Fixes
    • Improved database migrations by skipping blank statements and continuing safely when duplicate-column errors occur.
    • Added automatic recovery for missing additive database columns when the database opens.
    • Improved migration history updates to prevent duplicate records.
    • Other database errors continue to be reported normally.

1.10.1 曾把 session_parent/proxy/sort 编成 41/42/43,随后又在中间插入 thinking_blocks 与 cache_creation,把这三项改成 43/44/45。已升到旧 43 的库再跑新 44 会 duplicate column name: proxyEnabled,同时永远跳过 41/42 的新列。

让 ADD COLUMN 对 duplicate column 幂等,并在迁移/打开库时补齐缺失列。
@coderabbitai

coderabbitai Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2427e2c6-711a-4d9a-9476-e228e02d6884

📥 Commits

Reviewing files that changed from the base of the PR and between a7ae8bb and 1c554fd.

📒 Files selected for processing (4)
  • app/src/main/java/com/aicode/core/db/MigrationLoader.kt
  • app/src/main/java/com/aicode/core/db/SchemaCatchUp.kt
  • app/src/main/java/com/aicode/di/AgentModule.kt
  • app/src/test/java/com/aicode/core/db/FileMigrationTest.kt

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The PR adds additive SQLite schema repair for existing agent tables. File migrations tolerate duplicate-column errors, invoke schema repair, and replace migration history rows. The agent Room database also runs schema repair when it opens.

Changes

SQLite schema catch-up

Layer / File(s) Summary
Additive schema repair
app/src/main/java/com/aicode/core/db/SchemaCatchUp.kt, app/src/test/java/com/aicode/core/db/FileMigrationTest.kt
SchemaCatchUp detects and adds required columns for four tables. Tests cover duplicate-column messages and required columns from renumbered migrations.
Migration execution updates
app/src/main/java/com/aicode/core/db/MigrationLoader.kt
File migrations skip blank statements, continue after duplicate-column errors, invoke SchemaCatchUp.ensure, and use INSERT OR REPLACE for migration history.
Database open integration
app/src/main/java/com/aicode/di/AgentModule.kt
The agent Room database invokes SchemaCatchUp.ensure(db) from its onOpen callback.

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

Merge Risk: ⚪ Minimal · up to 1c554

The change makes historical database migrations idempotent and fills missing columns so affected upgrades can start normally while preserving existing data; no actionable merge-blocking risk remains beyond normal checks and review.

Sequence Diagram(s)

sequenceDiagram
  participant RoomDatabase
  participant RoomDatabaseCallback
  participant SchemaCatchUp
  participant SupportSQLiteDatabase
  RoomDatabase->>RoomDatabaseCallback: onOpen(db)
  RoomDatabaseCallback->>SchemaCatchUp: ensure(db)
  SchemaCatchUp->>SupportSQLiteDatabase: inspect tables and columns
  SchemaCatchUp->>SupportSQLiteDatabase: ALTER TABLE missing columns
Loading

Suggested reviewers: jieapi

🚥 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 11 functions across 4 files. 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 accurately summarizes the main change: supporting historical migration numbering and preventing duplicate column additions during startup.
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.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@MystHiru MystHiru closed this Aug 31, 2026
@MystHiru
MystHiru deleted the fix/db-migration-compat branch August 31, 2026 05:06
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