From 92472045d79b65dc0411adf364c7b3070cd63917 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 24 Apr 2012 01:27:28 +0900 Subject: [PATCH] fix importing of changed models and duplicate revlog/card entries --- anki/importing/anki2.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/anki/importing/anki2.py b/anki/importing/anki2.py index ebb693057..b07addfb8 100644 --- a/anki/importing/anki2.py +++ b/anki/importing/anki2.py @@ -101,6 +101,11 @@ class Anki2Importer(Importer): # Models ###################################################################### + # Models in the two decks may share an ID but not a schema, so we need to + # compare the field & template signature rather than just rely on ID. If + # we created a new model on a conflict then multiple imports would end up + # with lots of models however, so we store a list of "alternate versions" + # of a model in the model, so that importing a model is idempotent. def _prepareModels(self): "Prepare index of schema hashes." @@ -129,25 +134,28 @@ class Anki2Importer(Importer): return mid # if it does exist, do the schema match? dst = self.dst.models.get(mid) + shash = self.src.models.scmhash(src) dhash = self.src.models.scmhash(dst) - if self.src.models.scmhash(src) == dhash: + if shash == dhash: # reuse without modification self._modelMap[mid] = mid return mid # try any alternative versions - vers = src.get("vers") + vers = dst.get("vers") for v in vers: - m = self.src.models.get(v) - if self.src.models.scmhash(m) == dhash: + m = self.dst.models.get(v) + if self.dst.models.scmhash(m) == shash: # valid alternate found; use that self._modelMap[mid] = m['id'] return m['id'] # need to add a new alternate version, with new id - self.dst.models._add(src) + self.dst.models.add(src) if vers: dst['vers'].append(src['id']) else: dst['vers'] = [src['id']] + self.dst.models.save(dst) + return src['id'] # Decks ###################################################################### @@ -231,9 +239,9 @@ class Anki2Importer(Importer): cnt += 1 # apply self.dst.db.executemany(""" -insert into cards values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""", cards) +insert or ignore into cards values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""", cards) self.dst.db.executemany(""" -insert into revlog values (?,?,?,?,?,?,?,?,?)""", revlog) +insert or ignore into revlog values (?,?,?,?,?,?,?,?,?)""", revlog) self.log.append(_("%d cards imported.") % cnt) # Media