diff --git a/rslib/src/deckconfig/undo.rs b/rslib/src/deckconfig/undo.rs index cf14c8586..0a0ed7a2d 100644 --- a/rslib/src/deckconfig/undo.rs +++ b/rslib/src/deckconfig/undo.rs @@ -44,6 +44,13 @@ impl Collection { Ok(()) } + pub(crate) fn add_deck_config_if_unique_undoable(&mut self, config: &DeckConfig) -> Result<()> { + if self.storage.add_deck_conf_if_unique(config)? { + self.save_undo(UndoableDeckConfigChange::Added(Box::new(config.clone()))); + } + Ok(()) + } + pub(super) fn update_deck_config_undoable( &mut self, config: &DeckConfig, diff --git a/rslib/src/import_export/package/apkg/import.rs b/rslib/src/import_export/package/apkg/import.rs index 02e7e08f8..843c4fd83 100644 --- a/rslib/src/import_export/package/apkg/import.rs +++ b/rslib/src/import_export/package/apkg/import.rs @@ -41,7 +41,6 @@ struct Context<'a> { remapped_notes: HashMap, existing_notes: HashSet, remapped_decks: HashMap, - remapped_deck_configs: HashMap, data: ExchangeData, usn: Usn, /// Map of source media files, that do not already exist in the target. @@ -143,7 +142,6 @@ impl<'a> Context<'a> { remapped_notes: HashMap::new(), existing_notes, remapped_decks: HashMap::new(), - remapped_deck_configs: HashMap::new(), added_cards: HashSet::new(), used_media_entries: HashMap::new(), normalize_notes, @@ -345,12 +343,10 @@ impl<'a> Context<'a> { } fn import_deck_configs(&mut self) -> Result<()> { - // TODO: keep ids if possible? for mut config in mem::take(&mut self.data.deck_configs) { - let old_id = mem::take(&mut config.id); + config.usn = self.usn; self.target_col - .add_deck_config_inner(&mut config, Some(self.usn))?; - self.remapped_deck_configs.insert(old_id, config.id); + .add_deck_config_if_unique_undoable(&config)?; } Ok(()) } @@ -361,7 +357,6 @@ impl<'a> Context<'a> { for mut deck in mem::take(&mut self.data.decks) { deck.maybe_reparent(&renamed_parents); - self.remap_deck_config_id(&mut deck)?; if let Some(original) = self.get_deck_by_name(&deck)? { if original.is_filtered() { deck.uniquify_name(&mut renamed_parents); @@ -377,16 +372,6 @@ impl<'a> Context<'a> { Ok(()) } - fn remap_deck_config_id(&mut self, deck: &mut Deck) -> Result<()> { - if let Some(config_id) = self - .remapped_deck_configs - .get(&DeckConfigId(deck.normal()?.config_id)) - { - deck.normal_mut()?.config_id = config_id.0; - } - Ok(()) - } - fn add_deck(&mut self, deck: &mut Deck) -> Result<()> { let old_id = mem::take(&mut deck.id); self.target_col.add_deck_inner(deck, self.usn)?; diff --git a/rslib/src/storage/deckconfig/add_if_unique.sql b/rslib/src/storage/deckconfig/add_if_unique.sql new file mode 100644 index 000000000..516024466 --- /dev/null +++ b/rslib/src/storage/deckconfig/add_if_unique.sql @@ -0,0 +1,3 @@ +INSERT + OR IGNORE INTO deck_config (id, name, mtime_secs, usn, config) +VALUES (?, ?, ?, ?, ?); \ No newline at end of file diff --git a/rslib/src/storage/deckconfig/mod.rs b/rslib/src/storage/deckconfig/mod.rs index aeffc0151..c7b31dc92 100644 --- a/rslib/src/storage/deckconfig/mod.rs +++ b/rslib/src/storage/deckconfig/mod.rs @@ -67,6 +67,22 @@ impl SqliteStorage { Ok(()) } + pub(crate) fn add_deck_conf_if_unique(&self, conf: &DeckConfig) -> Result { + let mut conf_bytes = vec![]; + conf.inner.encode(&mut conf_bytes)?; + self.db + .prepare_cached(include_str!("add_if_unique.sql"))? + .execute(params![ + conf.id, + conf.name, + conf.mtime_secs, + conf.usn, + conf_bytes, + ]) + .map(|added| added == 1) + .map_err(Into::into) + } + pub(crate) fn update_deck_conf(&self, conf: &DeckConfig) -> Result<()> { let mut conf_bytes = vec![]; conf.inner.encode(&mut conf_bytes)?;