notes: make legacy notes migration append-only and retryable
- Commit
17dcbecc603f8957520535747a4b8768e72165ce- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 20:10 UTC (29 days ago)
- Parents
2ca314c
Symptom: two data-loss paths in the one-time legacy
("passwords-db") migration on first launch after upgrade.
Root cause: (1) MainActivity upserted legacy rows verbatim, so a
legacy primary key collided with a current notes-db row and the
@Upsert overwrote the current note. (2)
NotesRepository.readAndClearLegacyNotes deleted the legacy file in
a finally block, so a failed read (or a crash before the caller's
upsert) destroyed the only copy with no retry.
Fix: remap legacy rows to autogenerated ids (id = 0) so migration
only appends, and delete the legacy file only after a successful
read.
Verification: no new automated test; the legacy path needs a real
SQLCipher file database, which the Robolectric in-memory harness
(seedNotesDb) cannot stand up. Existing MainActivityTest and
NotesRepositoryTest legacy-empty cases still cover the no-op path;
full notes unit-test suite run at the end of this stack.Diff
notes/src/main/java/com/vayunmathur/notes/MainActivity.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/MainActivity.kt b/notes/src/main/java/com/vayunmathur/notes/MainActivity.ktindex c287a4415d..bf038873d3 100644--- a/notes/src/main/java/com/vayunmathur/notes/MainActivity.kt+++ b/notes/src/main/java/com/vayunmathur/notes/MainActivity.kt@@ -32,7 +32,10 @@ class MainActivity : MaActivity() { lifecycleScope.launch(Dispatchers.IO) { notesRepository = NotesRepository.get(applicationContext) val legacyNotes = notesRepository.readAndClearLegacyNotes()- if (legacyNotes.isNotEmpty()) notesRepository.upsertAll(legacyNotes)+ // Legacy rows carry primary keys from the old database file; writing+ // them verbatim would overwrite any current note with the same id.+ // Remap to autogenerated ids so migration only ever appends.+ if (legacyNotes.isNotEmpty()) notesRepository.upsertAll(legacyNotes.map { it.copy(id = 0) }) // One-time: img_<uuid>.jpg -> sha256.jpg content-addressed copy, then // rewrite block references and prune the superseded sources. val renames = NoteImageStore.migrateToContentAddressed(applicationContext)
notes/src/main/java/com/vayunmathur/notes/data/NotesRepository.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/data/NotesRepository.kt b/notes/src/main/java/com/vayunmathur/notes/data/NotesRepository.ktindex 917c53420d..7f165d7db2 100644--- a/notes/src/main/java/com/vayunmathur/notes/data/NotesRepository.kt+++ b/notes/src/main/java/com/vayunmathur/notes/data/NotesRepository.kt@@ -76,12 +76,17 @@ class NotesRepository private constructor(context: Context) : suspend fun readAndClearLegacyNotes(): List<Note> { if (!appContext.getDatabasePath(LEGACY_DB_NAME).exists()) return emptyList() val legacy = openAuxiliary(LEGACY_DB_NAME)- return try {+ // Delete only after a successful read: deleting in a finally would+ // destroy the only copy if getAll() throws (corrupt/encrypted legacy+ // file), and the caller upserts after this returns, so a failed read+ // must leave the file in place for a later retry.+ val notes = try { legacy.noteDao().getAll() } finally { legacy.close()- appContext.deleteDatabase(LEGACY_DB_NAME) }+ appContext.deleteDatabase(LEGACY_DB_NAME)+ return notes } companion object {