fix exporting of non-default deck configs

This commit is contained in:
Damien Elmes 2021-05-31 14:31:40 +10:00
parent bb2026f3f8
commit 25e4e4c8f6
7 changed files with 28 additions and 27 deletions

View File

@ -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(

View File

@ -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;

View File

@ -10,15 +10,12 @@ use crate::{
};
impl DeckConfigService for Backend {
fn add_or_update_deck_config_legacy(
&self,
input: pb::AddOrUpdateDeckConfigLegacyIn,
) -> Result<pb::DeckConfigId> {
let conf: DeckConfSchema11 = serde_json::from_slice(&input.config)?;
fn add_or_update_deck_config_legacy(&self, input: pb::Json) -> Result<pb::DeckConfigId> {
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 })
})
})

View File

@ -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(

View File

@ -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());
}

View File

@ -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);

View File

@ -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;
}