Fix importing cards/revlog

Card ids are manually uniquified.
This commit is contained in:
RumovZ 2022-04-10 09:22:41 +02:00
parent 126a92c239
commit e1899152be
2 changed files with 25 additions and 6 deletions

View file

@ -53,7 +53,8 @@ struct Context<'a> {
/// Source notes that cannot be imported, because notes with the same guid /// Source notes that cannot be imported, because notes with the same guid
/// exist in the target, but their notetypes don't match. /// exist in the target, but their notetypes don't match.
conflicting_notes: HashSet<NoteId>, conflicting_notes: HashSet<NoteId>,
added_cards: HashSet<CardId>, remapped_cards: HashMap<CardId, CardId>,
existing_cards: HashSet<CardId>,
normalize_notes: bool, normalize_notes: bool,
} }
@ -133,6 +134,7 @@ impl<'a> Context<'a> {
let usn = target_col.usn()?; let usn = target_col.usn()?;
let normalize_notes = target_col.get_config_bool(BoolKey::NormalizeNoteText); let normalize_notes = target_col.get_config_bool(BoolKey::NormalizeNoteText);
let existing_notes = target_col.storage.get_all_note_ids()?; let existing_notes = target_col.storage.get_all_note_ids()?;
let existing_cards = target_col.storage.get_all_card_ids()?;
Ok(Self { Ok(Self {
target_col, target_col,
archive, archive,
@ -144,7 +146,8 @@ impl<'a> Context<'a> {
remapped_notes: HashMap::new(), remapped_notes: HashMap::new(),
existing_notes, existing_notes,
remapped_decks: HashMap::new(), remapped_decks: HashMap::new(),
added_cards: HashSet::new(), remapped_cards: HashMap::new(),
existing_cards,
used_media_entries: HashMap::new(), used_media_entries: HashMap::new(),
normalize_notes, normalize_notes,
}) })
@ -422,9 +425,9 @@ impl<'a> Context<'a> {
fn add_card(&mut self, card: &mut Card) -> Result<()> { fn add_card(&mut self, card: &mut Card) -> Result<()> {
card.usn = self.usn; card.usn = self.usn;
if self.target_col.add_card_if_unique_undoable(card)? { self.uniquify_card_id(card);
self.added_cards.insert(card.id); self.target_col.add_card_if_unique_undoable(card)?;
} self.existing_cards.insert(card.id);
Ok(()) Ok(())
} }
@ -434,6 +437,14 @@ impl<'a> Context<'a> {
} }
} }
fn uniquify_card_id(&mut self, card: &mut Card) {
let original = card.id;
while self.existing_cards.contains(&card.id) {
card.id.0 += 999;
}
self.remapped_cards.insert(original, card.id);
}
fn remap_deck_id(&self, card: &mut Card) { fn remap_deck_id(&self, card: &mut Card) {
if let Some(did) = self.remapped_decks.get(&card.deck_id) { if let Some(did) = self.remapped_decks.get(&card.deck_id) {
card.deck_id = *did; card.deck_id = *did;
@ -442,7 +453,8 @@ impl<'a> Context<'a> {
fn import_revlog(&mut self) -> Result<()> { fn import_revlog(&mut self) -> Result<()> {
for mut entry in mem::take(&mut self.data.revlog) { for mut entry in mem::take(&mut self.data.revlog) {
if self.added_cards.contains(&entry.cid) { if let Some(cid) = self.remapped_cards.get(&entry.cid) {
entry.cid = *cid;
entry.usn = self.usn; entry.usn = self.usn;
self.target_col.add_revlog_entry_if_unique_undoable(entry)?; self.target_col.add_revlog_entry_if_unique_undoable(entry)?;
} }

View file

@ -428,6 +428,13 @@ impl super::SqliteStorage {
.collect() .collect()
} }
pub(crate) fn get_all_card_ids(&self) -> Result<HashSet<CardId>> {
self.db
.prepare("SELECT id FROM cards")?
.query_and_then([], |row| Ok(row.get(0)?))?
.collect()
}
pub(crate) fn all_cards_as_nid_and_ord(&self) -> Result<HashSet<(NoteId, u16)>> { pub(crate) fn all_cards_as_nid_and_ord(&self) -> Result<HashSet<(NoteId, u16)>> {
self.db self.db
.prepare("SELECT nid, ord FROM cards")? .prepare("SELECT nid, ord FROM cards")?