From d6706e1f0e7539fa658ac93e9285e4a19531fe72 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 24 Apr 2020 15:31:32 +1000 Subject: [PATCH] handle changed sort field index --- rslib/src/media/check.rs | 2 +- rslib/src/notes.rs | 13 ++++++++----- rslib/src/notetype/mod.rs | 6 +++++- rslib/src/notetype/schemachange.rs | 17 ++++++++++++++--- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/rslib/src/media/check.rs b/rslib/src/media/check.rs index d250722c8..5f33471b3 100644 --- a/rslib/src/media/check.rs +++ b/rslib/src/media/check.rs @@ -404,7 +404,7 @@ where &self.mgr.media_folder, )? { // note was modified, needs saving - note.prepare_for_update(nt, usn)?; + note.prepare_for_update(nt, Some(usn))?; self.ctx.storage.update_note(¬e)?; collection_modified = true; } diff --git a/rslib/src/notes.rs b/rslib/src/notes.rs index 05660dab1..dbfc78560 100644 --- a/rslib/src/notes.rs +++ b/rslib/src/notes.rs @@ -63,7 +63,8 @@ impl Note { Ok(()) } - pub fn prepare_for_update(&mut self, nt: &NoteType, usn: Usn) -> Result<()> { + /// Prepare note for saving to the database. If usn is provided, mtime will be bumped. + pub fn prepare_for_update(&mut self, nt: &NoteType, usn: Option) -> Result<()> { assert!(nt.id == self.ntid); if nt.fields.len() != self.fields.len() { return Err(AnkiError::invalid_input(format!( @@ -87,8 +88,10 @@ impl Note { }; self.sort_field = Some(sort_field.into()); self.checksum = Some(checksum); - self.mtime = TimestampSecs::now(); - self.usn = usn; + if let Some(usn) = usn { + self.mtime = TimestampSecs::now(); + self.usn = usn; + } Ok(()) } @@ -190,7 +193,7 @@ impl Collection { did: DeckID, ) -> Result<()> { self.canonify_note_tags(note, ctx.usn)?; - note.prepare_for_update(&ctx.notetype, ctx.usn)?; + note.prepare_for_update(&ctx.notetype, Some(ctx.usn))?; self.storage.add_note(note)?; self.generate_cards_for_new_note(ctx, note, did) } @@ -211,7 +214,7 @@ impl Collection { note: &mut Note, ) -> Result<()> { self.canonify_note_tags(note, ctx.usn)?; - note.prepare_for_update(ctx.notetype, ctx.usn)?; + note.prepare_for_update(ctx.notetype, Some(ctx.usn))?; self.generate_cards_for_existing_note(ctx, note)?; self.storage.update_note(note)?; diff --git a/rslib/src/notetype/mod.rs b/rslib/src/notetype/mod.rs index 6c39ca7b1..a7f06a857 100644 --- a/rslib/src/notetype/mod.rs +++ b/rslib/src/notetype/mod.rs @@ -328,7 +328,11 @@ impl Collection { } self.transact(None, |col| { if let Some(existing_notetype) = existing { - col.update_notes_for_changed_fields(nt, existing_notetype.fields.len())?; + col.update_notes_for_changed_fields( + nt, + existing_notetype.fields.len(), + existing_notetype.config.sort_field_idx, + )?; col.update_cards_for_changed_templates(nt, existing_notetype.templates.len())?; } diff --git a/rslib/src/notetype/schemachange.rs b/rslib/src/notetype/schemachange.rs index 43164f438..e8c843bc6 100644 --- a/rslib/src/notetype/schemachange.rs +++ b/rslib/src/notetype/schemachange.rs @@ -54,11 +54,22 @@ impl Collection { &mut self, nt: &NoteType, previous_field_count: usize, + previous_sort_idx: u32, ) -> Result<()> { let ords: Vec<_> = nt.fields.iter().map(|f| f.ord).collect(); if !ords_changed(&ords, previous_field_count) { - // nothing to do - return Ok(()); + if nt.config.sort_field_idx == previous_sort_idx { + // only need to update sort field + let nids = self.search_notes_only(&format!("mid:{}", nt.id))?; + for nid in nids { + let mut note = self.storage.get_note(nid)?.unwrap(); + note.prepare_for_update(nt, None)?; + self.storage.update_note(¬e)?; + } + } else { + // nothing to do + return Ok(()); + } } self.storage.set_schema_modified()?; @@ -81,7 +92,7 @@ impl Collection { }) .map(Into::into) .collect(); - note.prepare_for_update(nt, usn)?; + note.prepare_for_update(nt, Some(usn))?; self.storage.update_note(¬e)?; } Ok(())