mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 15:32:23 -04:00
Keep source ids of imported deck configs (or skip)
This commit is contained in:
parent
7798b78c7d
commit
a528106af0
4 changed files with 28 additions and 17 deletions
|
@ -44,6 +44,13 @@ impl Collection {
|
||||||
Ok(())
|
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(
|
pub(super) fn update_deck_config_undoable(
|
||||||
&mut self,
|
&mut self,
|
||||||
config: &DeckConfig,
|
config: &DeckConfig,
|
||||||
|
|
|
@ -41,7 +41,6 @@ struct Context<'a> {
|
||||||
remapped_notes: HashMap<NoteId, NoteId>,
|
remapped_notes: HashMap<NoteId, NoteId>,
|
||||||
existing_notes: HashSet<NoteId>,
|
existing_notes: HashSet<NoteId>,
|
||||||
remapped_decks: HashMap<DeckId, DeckId>,
|
remapped_decks: HashMap<DeckId, DeckId>,
|
||||||
remapped_deck_configs: HashMap<DeckConfigId, DeckConfigId>,
|
|
||||||
data: ExchangeData,
|
data: ExchangeData,
|
||||||
usn: Usn,
|
usn: Usn,
|
||||||
/// Map of source media files, that do not already exist in the target.
|
/// 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(),
|
remapped_notes: HashMap::new(),
|
||||||
existing_notes,
|
existing_notes,
|
||||||
remapped_decks: HashMap::new(),
|
remapped_decks: HashMap::new(),
|
||||||
remapped_deck_configs: HashMap::new(),
|
|
||||||
added_cards: HashSet::new(),
|
added_cards: HashSet::new(),
|
||||||
used_media_entries: HashMap::new(),
|
used_media_entries: HashMap::new(),
|
||||||
normalize_notes,
|
normalize_notes,
|
||||||
|
@ -345,12 +343,10 @@ impl<'a> Context<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn import_deck_configs(&mut self) -> Result<()> {
|
fn import_deck_configs(&mut self) -> Result<()> {
|
||||||
// TODO: keep ids if possible?
|
|
||||||
for mut config in mem::take(&mut self.data.deck_configs) {
|
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
|
self.target_col
|
||||||
.add_deck_config_inner(&mut config, Some(self.usn))?;
|
.add_deck_config_if_unique_undoable(&config)?;
|
||||||
self.remapped_deck_configs.insert(old_id, config.id);
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -361,7 +357,6 @@ impl<'a> Context<'a> {
|
||||||
|
|
||||||
for mut deck in mem::take(&mut self.data.decks) {
|
for mut deck in mem::take(&mut self.data.decks) {
|
||||||
deck.maybe_reparent(&renamed_parents);
|
deck.maybe_reparent(&renamed_parents);
|
||||||
self.remap_deck_config_id(&mut deck)?;
|
|
||||||
if let Some(original) = self.get_deck_by_name(&deck)? {
|
if let Some(original) = self.get_deck_by_name(&deck)? {
|
||||||
if original.is_filtered() {
|
if original.is_filtered() {
|
||||||
deck.uniquify_name(&mut renamed_parents);
|
deck.uniquify_name(&mut renamed_parents);
|
||||||
|
@ -377,16 +372,6 @@ impl<'a> Context<'a> {
|
||||||
Ok(())
|
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<()> {
|
fn add_deck(&mut self, deck: &mut Deck) -> Result<()> {
|
||||||
let old_id = mem::take(&mut deck.id);
|
let old_id = mem::take(&mut deck.id);
|
||||||
self.target_col.add_deck_inner(deck, self.usn)?;
|
self.target_col.add_deck_inner(deck, self.usn)?;
|
||||||
|
|
3
rslib/src/storage/deckconfig/add_if_unique.sql
Normal file
3
rslib/src/storage/deckconfig/add_if_unique.sql
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
INSERT
|
||||||
|
OR IGNORE INTO deck_config (id, name, mtime_secs, usn, config)
|
||||||
|
VALUES (?, ?, ?, ?, ?);
|
|
@ -67,6 +67,22 @@ impl SqliteStorage {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn add_deck_conf_if_unique(&self, conf: &DeckConfig) -> Result<bool> {
|
||||||
|
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<()> {
|
pub(crate) fn update_deck_conf(&self, conf: &DeckConfig) -> Result<()> {
|
||||||
let mut conf_bytes = vec![];
|
let mut conf_bytes = vec![];
|
||||||
conf.inner.encode(&mut conf_bytes)?;
|
conf.inner.encode(&mut conf_bytes)?;
|
||||||
|
|
Loading…
Reference in a new issue