fix(core): serialize concurrent database migrations - #13067
Conversation
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (3 files)
The fix is sound: |
|
Since this fixes the issue, will the database corruption be fixed when this lands in a kilo update? My existing database got corrupted due to this issue. |
|
Yes. The next release containing this fix should open your existing database normally if this issue caused the startup failure. The error message you shared does not indicate that the database was corrupted. If you encounter any other errors after updating, please let us know and include the complete error message. |
Issue
Fixes #13060
Context
Kilo 7.4.21 can crash during startup when two processes share the same SQLite database and attempt the same pending migration.
applyOnly()previously took one completed-migration snapshot, so a second process could record an ID after that snapshot; the first process would replay the migration and then fail its journal insert withUNIQUE constraint failed: migration.id.Implementation
Run each pending migration in a
BEGIN IMMEDIATEtransaction and re-check that migration's journal ID after acquiring the SQLite write reservation. If another process already recorded it, skipup(); otherwise apply the migration and journal it atomically.The regression coverage verifies both the stale-snapshot skip and that the write reservation is held before migration work begins. A patch changeset documents the user-facing startup fix.
Screenshots / Video
N/A
How to Test
Manual/local verification
INSERTwithSQLITE_CONSTRAINT_PRIMARYKEY/UNIQUE constraint failed: migration.id.cd packages/core && bun test test/kilocode/database-migration-compat.test.ts test/database-migration.test.ts(21 passed, 0 failed).cd packages/core && bun run typecheck.bun run script/check-opencode-annotations.ts --worktree.git diff --check.bun turbo typecheck --filter=!@kilocode/kilo-jetbrainssuccessfully.Reviewer test steps
cd packages/core.bun test test/kilocode/database-migration-compat.test.ts test/database-migration.test.ts.SQLITE_BUSYwhile the migration transaction holds its immediate write reservation.Checklist