From 25e4e4c8f6515e59c154ef2da06bbda3aca8097a Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 31 May 2021 14:31:40 +1000 Subject: [PATCH] fix exporting of non-default deck configs --- pylib/anki/decks.py | 3 ++- rslib/backend.proto | 8 +------- rslib/src/backend/deckconfig.rs | 9 +++------ rslib/src/deckconfig/mod.rs | 29 +++++++++++++++++++---------- rslib/src/deckconfig/update.rs | 2 +- rslib/src/decks/tree.rs | 2 +- rslib/src/sync/mod.rs | 2 +- 7 files changed, 28 insertions(+), 27 deletions(-) diff --git a/pylib/anki/decks.py b/pylib/anki/decks.py index 5718ed0b1..d5788d15b 100644 --- a/pylib/anki/decks.py +++ b/pylib/anki/decks.py @@ -357,8 +357,9 @@ class DeckManager: return None def update_config(self, conf: DeckConfigDict, preserve_usn: bool = False) -> None: + "preserve_usn is ignored" conf["id"] = self.col._backend.add_or_update_deck_config_legacy( - config=to_json_bytes(conf), preserve_usn_and_mtime=preserve_usn + json=to_json_bytes(conf) ) def add_config( diff --git a/rslib/backend.proto b/rslib/backend.proto index e61514214..1fe3b61c6 100644 --- a/rslib/backend.proto +++ b/rslib/backend.proto @@ -230,8 +230,7 @@ service CardRenderingService { } service DeckConfigService { - rpc AddOrUpdateDeckConfigLegacy(AddOrUpdateDeckConfigLegacyIn) - returns (DeckConfigId); + rpc AddOrUpdateDeckConfigLegacy(Json) returns (DeckConfigId); rpc GetDeckConfig(DeckConfigId) returns (DeckConfig); rpc AllDeckConfigLegacy(Empty) returns (Json); rpc GetDeckConfigLegacy(DeckConfigId) returns (Json); @@ -928,11 +927,6 @@ message CloseCollectionIn { bool downgrade_to_schema11 = 1; } -message AddOrUpdateDeckConfigLegacyIn { - bytes config = 1; - bool preserve_usn_and_mtime = 2; -} - message DeckConfigsForUpdate { message ConfigWithExtra { DeckConfig config = 1; diff --git a/rslib/src/backend/deckconfig.rs b/rslib/src/backend/deckconfig.rs index 5e9590a4c..6534d8fc7 100644 --- a/rslib/src/backend/deckconfig.rs +++ b/rslib/src/backend/deckconfig.rs @@ -10,15 +10,12 @@ use crate::{ }; impl DeckConfigService for Backend { - fn add_or_update_deck_config_legacy( - &self, - input: pb::AddOrUpdateDeckConfigLegacyIn, - ) -> Result { - let conf: DeckConfSchema11 = serde_json::from_slice(&input.config)?; + fn add_or_update_deck_config_legacy(&self, input: pb::Json) -> Result { + let conf: DeckConfSchema11 = serde_json::from_slice(&input.json)?; let mut conf: DeckConfig = conf.into(); self.with_col(|col| { col.transact_no_undo(|col| { - col.add_or_update_deck_config(&mut conf, input.preserve_usn_and_mtime)?; + col.add_or_update_deck_config_legacy(&mut conf)?; Ok(pb::DeckConfigId { dcid: conf.id.0 }) }) }) diff --git a/rslib/src/deckconfig/mod.rs b/rslib/src/deckconfig/mod.rs index 5106aab8e..46f40d97d 100644 --- a/rslib/src/deckconfig/mod.rs +++ b/rslib/src/deckconfig/mod.rs @@ -111,16 +111,8 @@ impl Collection { } impl Collection { - pub(crate) fn add_or_update_deck_config( - &mut self, - config: &mut DeckConfig, - preserve_usn_and_mtime: bool, - ) -> Result<()> { - let usn = if preserve_usn_and_mtime { - None - } else { - Some(self.usn()?) - }; + pub(crate) fn add_or_update_deck_config(&mut self, config: &mut DeckConfig) -> Result<()> { + let usn = Some(self.usn()?); if config.id.0 == 0 { self.add_deck_config_inner(config, usn) @@ -133,6 +125,23 @@ impl Collection { } } + /// Used by the old import code; if provided id is non-zero, will add + /// instead of ignoring. Does not support undo. + pub(crate) fn add_or_update_deck_config_legacy( + &mut self, + config: &mut DeckConfig, + ) -> Result<()> { + let usn = Some(self.usn()?); + + if config.id.0 == 0 { + self.add_deck_config_inner(config, usn) + } else { + config.set_modified(usn.unwrap()); + self.storage + .add_or_update_deck_config_with_existing_id(config) + } + } + /// Assigns an id and adds to DB. If usn is provided, modification time and /// usn will be updated. pub(crate) fn add_deck_config_inner( diff --git a/rslib/src/deckconfig/update.rs b/rslib/src/deckconfig/update.rs index f6c66c6da..2e73b59e4 100644 --- a/rslib/src/deckconfig/update.rs +++ b/rslib/src/deckconfig/update.rs @@ -121,7 +121,7 @@ impl Collection { // add/update provided configs for conf in &mut input.configs { - self.add_or_update_deck_config(conf, false)?; + self.add_or_update_deck_config(conf)?; configs_after_update.insert(conf.id, conf.clone()); } diff --git a/rslib/src/decks/tree.rs b/rslib/src/decks/tree.rs index be3388bd6..77951b2db 100644 --- a/rslib/src/decks/tree.rs +++ b/rslib/src/decks/tree.rs @@ -428,7 +428,7 @@ mod test { // set the limit to 4, which should mean 3 are left let mut conf = col.get_deck_config(DeckConfigId(1), false)?.unwrap(); conf.inner.new_per_day = 4; - col.add_or_update_deck_config(&mut conf, false)?; + col.add_or_update_deck_config(&mut conf)?; let tree = col.deck_tree(Some(TimestampSecs::now()), None)?; assert_eq!(tree.children[0].new_count, 3); diff --git a/rslib/src/sync/mod.rs b/rslib/src/sync/mod.rs index 284b60d71..06c130b66 100644 --- a/rslib/src/sync/mod.rs +++ b/rslib/src/sync/mod.rs @@ -1378,7 +1378,7 @@ mod test { name: "new dconf".into(), ..Default::default() }; - col1.add_or_update_deck_config(&mut dconf, false)?; + col1.add_or_update_deck_config(&mut dconf)?; if let DeckKind::Normal(deck) = &mut deck.kind { deck.config_id = dconf.id.0; }