flashcards: move review logs with notes when notes change decks
- Commit
afcbd499990f58f129dcb547f9a439410ea9e6b1- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:21 UTC (28 days ago)
- Parents
4c0cd95
Moving notes to another deck (bulk move, or re-saving a note into a different deck) moved the notes and their cards but left each card's ReviewLog rows pointing at the old deck. ReviewLog.deckId is the deck attribution consumed by per-deck stats, the FSRS optimizer (which trains per deck), and deck deletion (which snapshots then deletes logs by deckId), so moved cards' history polluted the old deck: its stats and optimizer training included reviews that no longer belong to it, and deleting the target deck later left those logs orphaned under the old deck as phantom reviews. Add ReviewLogDao.moveByNotes (mirroring CardDao.moveByNotes) plus a moveReviewLogsByNotesImpl repository wrapper, and call it from both deck-changing paths for existing cards: moveNotesImpl (bulk move) and saveNoteImpl's misplaced-cards repair (edit-save deck change). Grade, undo, regeneration, and APKG import need no change: grading writes the log with the card's current deck, undo restores snapshotted deckIds, and regeneration/import create cards with no logs. Add moveNotesCarriesReviewLogsToTargetDeck: seeds a reviewed card, moves its note, asserts the log follows to the target deck, then deletes the target deck and asserts no orphan log remains under the old deck. Verified with :flashcards:testDevUnitTest — full suite green (19 classes, 157 tests, 0 failures). The new test fails without the fix (log never arrives under the target deck) and passes with it.
Diff
flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsDatabase.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsDatabase.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsDatabase.ktindex 6198633c58..2ac7e40559 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsDatabase.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsDatabase.kt@@ -223,6 +223,9 @@ interface ReviewLogDao { @Query("DELETE FROM ReviewLog WHERE cardId IN (:cardIds)") suspend fun deleteByCards(cardIds: List<Long>) + @Query("UPDATE ReviewLog SET deckId = :deckId WHERE cardId IN (SELECT id FROM Card WHERE noteId IN (:noteIds))")+ suspend fun moveByNotes(noteIds: List<Long>, deckId: Long)+ @Query("DELETE FROM ReviewLog WHERE deckId = :deckId") suspend fun deleteByDeck(deckId: Long) }
flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsRepositoryCardOps.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsRepositoryCardOps.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsRepositoryCardOps.ktindex 3039be5397..0e9e18815d 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsRepositoryCardOps.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsRepositoryCardOps.kt@@ -59,5 +59,7 @@ internal suspend fun FlashcardsRepository.insertReviewLogImpl(value: ReviewLog): internal suspend fun FlashcardsRepository.deleteReviewLogByIdImpl(id: Long) = reviewLogDao.deleteById(id) internal suspend fun FlashcardsRepository.deleteReviewLogsByCardsImpl(cardIds: List<Long>) = reviewLogDao.deleteByCards(cardIds)+internal suspend fun FlashcardsRepository.moveReviewLogsByNotesImpl(noteIds: List<Long>, deckId: Long) =+ reviewLogDao.moveByNotes(noteIds, deckId) internal suspend fun FlashcardsRepository.deleteReviewLogsByDeckImpl(deckId: Long) = reviewLogDao.deleteByDeck(deckId)
flashcards/src/main/java/com/vayunmathur/flashcards/util/FlashcardsNoteOps.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/util/FlashcardsNoteOps.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/util/FlashcardsNoteOps.ktindex 10160dfe87..fd2e94490b 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/util/FlashcardsNoteOps.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/util/FlashcardsNoteOps.kt@@ -29,6 +29,7 @@ import com.vayunmathur.flashcards.data.getReviewLogsByCardImpl import com.vayunmathur.flashcards.data.getTemplatesForNoteTypeImpl import com.vayunmathur.flashcards.data.insertReviewLogImpl import com.vayunmathur.flashcards.data.moveCardsByNotesImpl+import com.vayunmathur.flashcards.data.moveReviewLogsByNotesImpl import com.vayunmathur.flashcards.data.regenerateCardsImpl import com.vayunmathur.flashcards.data.setCardsSuspendedByNotesImpl import com.vayunmathur.flashcards.data.setCardsSuspendedImpl@@ -79,6 +80,8 @@ internal fun FlashcardsViewModel.saveNoteImpl( val misplaced = repository.getCardsByNoteImpl(finalNote.id).filter { it.deckId != finalNote.deckId } if (misplaced.isNotEmpty()) { repository.upsertCardsImpl(misplaced.map { it.copy(deckId = finalNote.deckId) })+ // Their review history moves with them (see moveNotesImpl).+ repository.moveReviewLogsByNotesImpl(listOf(finalNote.id), finalNote.deckId) } } @@ -151,6 +154,11 @@ internal fun FlashcardsViewModel.moveNotesImpl(noteIds: List<Long>, deckId: Long } repository.upsertNotesImpl(moved) repository.moveCardsByNotesImpl(noteIds, deckId)+ // ReviewLog.deckId is the deck attribution used by per-deck stats,+ // the FSRS optimizer, and deck deletion: without this, moved cards'+ // history stays behind (and deleting the target deck later orphans+ // those logs under the old deck as phantom reviews).+ repository.moveReviewLogsByNotesImpl(noteIds, deckId) } internal fun FlashcardsViewModel.addTagImpl(noteIds: List<Long>, tag: String) = launchIoImpl {
flashcards/src/test/java/com/vayunmathur/flashcards/util/FlashcardsViewModelTest.kt
diff --git a/flashcards/src/test/java/com/vayunmathur/flashcards/util/FlashcardsViewModelTest.kt b/flashcards/src/test/java/com/vayunmathur/flashcards/util/FlashcardsViewModelTest.ktindex d5b119c648..c08d14fd14 100644--- a/flashcards/src/test/java/com/vayunmathur/flashcards/util/FlashcardsViewModelTest.kt+++ b/flashcards/src/test/java/com/vayunmathur/flashcards/util/FlashcardsViewModelTest.kt@@ -185,6 +185,27 @@ class FlashcardsViewModelTest { } } + @Test+ fun moveNotesCarriesReviewLogsToTargetDeck() {+ val from = uniqueDeck("LogMoveFrom"); val to = uniqueDeck("LogMoveTo")+ val note = saveBasicNote(from.id, "LogMoveFront", "LogMoveBack")+ val card = runBlocking { repo.getCardsByNoteImpl(note.id) }.single()+ runBlocking {+ repo.insertReviewLogImpl(ReviewLog(cardId = card.id, deckId = from.id, reviewedAt = 1, grade = 3, elapsedDays = 1.0, scheduledDays = 2.0, state = 2))+ }+ vm.moveNotesImpl(listOf(note.id), to.id)+ await("logs moved") {+ runBlocking { repo.getReviewLogsByDeckOrderedImpl(to.id) }.any { it.cardId == card.id }+ }+ assertTrue(runBlocking { repo.getReviewLogsByDeckOrderedImpl(from.id) }.none { it.cardId == card.id })+ // Deleting the target deck must take the moved history with it —+ // otherwise orphaned logs linger under the old deck and pollute+ // its stats and FSRS optimization.+ vm.deleteDeckImpl(to)+ await("target deck deleted") { vm.decks.value.none { it.id == to.id } }+ assertTrue(runBlocking { repo.getReviewLogsByDeckOrderedImpl(from.id) }.none { it.cardId == card.id })+ }+ @Test fun bulkNoteOpsTagsMoveSuspendResetAndDelete() { val deck = uniqueDeck("BulkDeck"); val target = uniqueDeck("BulkTarget")