diff --git a/rslib/src/notetype/mod.rs b/rslib/src/notetype/mod.rs index db1aaff35..83a4bb905 100644 --- a/rslib/src/notetype/mod.rs +++ b/rslib/src/notetype/mod.rs @@ -322,12 +322,11 @@ impl Collection { pub fn update_notetype(&mut self, nt: &mut NoteType, preserve_usn: bool) -> Result<()> { let existing = self.get_notetype(nt.id)?; nt.prepare_for_update(existing.as_ref().map(AsRef::as_ref))?; - if !preserve_usn { - nt.mtime_secs = TimestampSecs::now(); - nt.usn = self.usn()?; - } self.transact(None, |col| { if let Some(existing_notetype) = existing { + if existing_notetype.mtime_secs > nt.mtime_secs { + return Err(AnkiError::invalid_input("attempt to save stale notetype")); + } col.update_notes_for_changed_fields( nt, existing_notetype.fields.len(), @@ -336,6 +335,11 @@ impl Collection { col.update_cards_for_changed_templates(nt, existing_notetype.templates.len())?; } + if !preserve_usn { + nt.mtime_secs = TimestampSecs::now(); + nt.usn = col.usn()?; + } + col.storage.update_notetype_config(&nt)?; col.storage.update_notetype_fields(nt.id, &nt.fields)?; col.storage