diff --git a/rslib/src/import_export/package/apkg/import.rs b/rslib/src/import_export/package/apkg/import.rs index 7acee2cd7..1edd16168 100644 --- a/rslib/src/import_export/package/apkg/import.rs +++ b/rslib/src/import_export/package/apkg/import.rs @@ -434,7 +434,7 @@ impl<'a> Context<'a> { for mut entry in mem::take(&mut self.data.revlog) { if self.added_cards.contains(&entry.cid) { entry.usn = self.usn; - self.target_col.add_revlog_entry_undoable(entry)?; + self.target_col.add_revlog_entry_if_unique_undoable(entry)?; } } Ok(()) diff --git a/rslib/src/revlog/undo.rs b/rslib/src/revlog/undo.rs index 24ea6f61a..71602f0cc 100644 --- a/rslib/src/revlog/undo.rs +++ b/rslib/src/revlog/undo.rs @@ -28,9 +28,17 @@ impl Collection { /// Add the provided revlog entry, modifying the ID if it is not unique. pub(crate) fn add_revlog_entry_undoable(&mut self, mut entry: RevlogEntry) -> Result { - entry.id = self.storage.add_revlog_entry(&entry, true)?; + entry.id = self.storage.add_revlog_entry(&entry, true)?.unwrap(); let id = entry.id; self.save_undo(UndoableRevlogChange::Added(Box::new(entry))); Ok(id) } + + /// Add the provided revlog entry, if its ID is unique. + pub(crate) fn add_revlog_entry_if_unique_undoable(&mut self, entry: RevlogEntry) -> Result<()> { + if self.storage.add_revlog_entry(&entry, false)?.is_some() { + self.save_undo(UndoableRevlogChange::Added(Box::new(entry))); + } + Ok(()) + } } diff --git a/rslib/src/storage/revlog/mod.rs b/rslib/src/storage/revlog/mod.rs index 27ee78ed8..d18d321e4 100644 --- a/rslib/src/storage/revlog/mod.rs +++ b/rslib/src/storage/revlog/mod.rs @@ -61,16 +61,19 @@ impl SqliteStorage { Ok(()) } - /// Returns the used id, which may differ if `ensure_unique` is true. + /// Adds the entry, if its id is unique. If it is not, and `uniquify` is true, + /// adds it with a new id. Returns the added id. + /// (I.e., the option is safe to unwrap, if `uniquify` is true.) pub(crate) fn add_revlog_entry( &self, entry: &RevlogEntry, - ensure_unique: bool, - ) -> Result { - self.db + uniquify: bool, + ) -> Result> { + let added = self + .db .prepare_cached(include_str!("add.sql"))? .execute(params![ - ensure_unique, + uniquify, entry.id, entry.cid, entry.usn, @@ -81,7 +84,7 @@ impl SqliteStorage { entry.taken_millis, entry.review_kind as u8 ])?; - Ok(RevlogId(self.db.last_insert_rowid())) + Ok((added > 0).then(|| RevlogId(self.db.last_insert_rowid()))) } pub(crate) fn get_revlog_entry(&self, id: RevlogId) -> Result> {