diff --git a/anki/importing/__init__.py b/anki/importing/__init__.py index fc601ca6e..71a0ebd51 100644 --- a/anki/importing/__init__.py +++ b/anki/importing/__init__.py @@ -341,12 +341,10 @@ where factId in (%s)""" % ",".join([str(s) for s in factIds])) from anki.importing.csvfile import TextImporter from anki.importing.anki10 import Anki10Importer -from anki.importing.mnemosyne10 import Mnemosyne10Importer from anki.importing.supermemo_xml import SupermemoXmlImporter Importers = ( (_("Text separated by tabs or semicolons (*)"), TextImporter), (_("Anki Deck (*.anki)"), Anki10Importer), - (_("Mnemosyne Deck (*.mem)"), Mnemosyne10Importer), (_("Supermemo XML export (*.xml)"), SupermemoXmlImporter), ) diff --git a/anki/migration/__init__.py b/anki/migration/__init__.py deleted file mode 100644 index ea8ce5786..000000000 --- a/anki/migration/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright: Damien Elmes -# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html - -class Migrator(object): - - def __init__(self, deck): - pass diff --git a/anki/migration/checker.py b/anki/migration/checker.py deleted file mode 100644 index 43bb98f96..000000000 --- a/anki/migration/checker.py +++ /dev/null @@ -1,68 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright: Damien Elmes -# License: GNU AGPL, version 3 or later; http://www.gnu.org/copyleft/agpl.html - -from anki.db import DB - -def check(path): - "True if deck looks ok." - db = DB(path) - # corrupt? - try: - if db.scalar("pragma integrity_check") != "ok": - return - except: - return - # old version? - if db.scalar("select version from decks") != 65: - return - # fields missing a field model? - if db.list(""" -select id from fields where fieldModelId not in ( -select distinct id from fieldModels)"""): - return - # facts missing a field? - if db.list(""" -select distinct facts.id from facts, fieldModels where -facts.modelId = fieldModels.modelId and fieldModels.id not in -(select fieldModelId from fields where factId = facts.id)"""): - return - # cards missing a fact? - if db.list(""" -select id from cards where factId not in (select id from facts)"""): - return - # cards missing a card model? - if db.list(""" -select id from cards where cardModelId not in -(select id from cardModels)"""): - return - # cards with a card model from the wrong model? - if db.list(""" -select id from cards where cardModelId not in (select cm.id from -cardModels cm, facts f where cm.modelId = f.modelId and -f.id = cards.factId)"""): - return - # facts missing a card? - if db.list(""" -select facts.id from facts -where facts.id not in (select distinct factId from cards)"""): - return - # dangling fields? - if db.list(""" -select id from fields where factId not in (select id from facts)"""): - return - # fields without matching interval - if db.list(""" -select id from fields where ordinal != (select ordinal from fieldModels -where id = fieldModelId)"""): - return - # incorrect types - if db.list(""" -select id from cards where relativeDelay != (case -when successive then 1 when reps then 0 else 2 end)"""): - return - if db.list(""" -select id from cards where type != (case -when type >= 0 then relativeDelay else relativeDelay - 3 end)"""): - return - return True diff --git a/anki/migration/upgrader.py b/anki/upgrade.py similarity index 84% rename from anki/migration/upgrader.py rename to anki/upgrade.py index 60bb657b9..f13d00364 100644 --- a/anki/migration/upgrader.py +++ b/anki/upgrade.py @@ -14,9 +14,9 @@ from anki.storage import _addSchema, _getDeckVars, _addDeckVars, \ # # Upgrading is the first step in migrating to 2.0. The ids are temporary and # may not be unique across multiple decks. After each of a user's v1.2 decks -# are upgraded, they need to be merged. +# are upgraded, they need to be merged via the import code. # -# Caller should have called check() on path before using this. +# Caller should have called check() on path before calling upgrade(). # class Upgrader(object): @@ -24,6 +24,9 @@ class Upgrader(object): def __init__(self): pass + # Upgrading + ###################################################################### + def upgrade(self, path): self.path = path self._openDB(path) @@ -32,6 +35,78 @@ class Upgrader(object): self._upgradeDeck() return self.deck + # Integrity checking + ###################################################################### + + def check(self, path): + "True if deck looks ok." + with DB(path) as db: + return self._check(db) + + def _check(self, db): + # corrupt? + try: + if db.scalar("pragma integrity_check") != "ok": + return + except: + return + # old version? + if db.scalar("select version from decks") != 65: + return + # fields missing a field model? + if db.list(""" + select id from fields where fieldModelId not in ( + select distinct id from fieldModels)"""): + return + # facts missing a field? + if db.list(""" + select distinct facts.id from facts, fieldModels where + facts.modelId = fieldModels.modelId and fieldModels.id not in + (select fieldModelId from fields where factId = facts.id)"""): + return + # cards missing a fact? + if db.list(""" + select id from cards where factId not in (select id from facts)"""): + return + # cards missing a card model? + if db.list(""" + select id from cards where cardModelId not in + (select id from cardModels)"""): + return + # cards with a card model from the wrong model? + if db.list(""" + select id from cards where cardModelId not in (select cm.id from + cardModels cm, facts f where cm.modelId = f.modelId and + f.id = cards.factId)"""): + return + # facts missing a card? + if db.list(""" + select facts.id from facts + where facts.id not in (select distinct factId from cards)"""): + return + # dangling fields? + if db.list(""" + select id from fields where factId not in (select id from facts)"""): + return + # fields without matching interval + if db.list(""" + select id from fields where ordinal != (select ordinal from fieldModels + where id = fieldModelId)"""): + return + # incorrect types + if db.list(""" + select id from cards where relativeDelay != (case + when successive then 1 when reps then 0 else 2 end)"""): + return + if db.list(""" + select id from cards where type != (case + when type >= 0 then relativeDelay else relativeDelay - 3 end)"""): + return + return True + + # DB/Deck opening + ###################################################################### + def _openDB(self, path): self.tmppath = namedtmp(os.path.basename(path)) shutil.copy(path, self.tmppath) diff --git a/tests/test_migration.py b/tests/test_upgrade.py similarity index 81% rename from tests/test_migration.py rename to tests/test_upgrade.py index 2d36586cc..e1d66d19a 100644 --- a/tests/test_migration.py +++ b/tests/test_upgrade.py @@ -3,15 +3,15 @@ import datetime from anki.consts import * from shared import getUpgradeDeckPath -from anki.migration.checker import check -from anki.migration.upgrader import Upgrader +from anki.upgrade import Upgrader -def test_checker(): +def test_check(): dst = getUpgradeDeckPath() - assert check(dst) + u = Upgrader() + assert u.check(dst) # if it's corrupted, will fail open(dst, "w+").write("foo") - assert not check(dst) + assert not u.check(dst) def test_upgrade(): dst = getUpgradeDeckPath()