From 2ee3ec8b7a60e5c218ec2b2f3a2f5e7bc790731a Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 9 Sep 2012 06:06:55 +0900 Subject: [PATCH] delete notes that are missing their model --- anki/collection.py | 17 ++++++++++++++++- anki/models.py | 3 +++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/anki/collection.py b/anki/collection.py index d2cecebf5..06075e731 100644 --- a/anki/collection.py +++ b/anki/collection.py @@ -626,10 +626,25 @@ where c.nid == f.id oldSize = os.stat(self.path)[stat.ST_SIZE] if self.db.scalar("pragma integrity_check") != "ok": return (_("Collection is corrupt. Please see the manual."), False) + # note types with a missing model + ids = self.db.list(""" +select id from notes where mid not in """ + ids2str(self.models.ids())) + if ids: + print self.db.list("select distinct mid from notes where id in " + ids2str(ids)) + problems.append( + ngettext("Deleted %d note with missing note type.", + "Deleted %d notes with missing note type.", len(ids)) + % len(ids)) + self.remNotes(ids) # delete any notes with missing cards ids = self.db.list(""" select id from notes where id not in (select distinct nid from cards)""") - self._remNotes(ids) + if ids: + cnt = len(ids) + problems.append( + ngettext("Deleted %d note with no cards.", + "Deleted %d notes with no cards.", cnt) % cnt) + self._remNotes(ids) # tags self.tags.registerNotes() # field cache diff --git a/anki/models.py b/anki/models.py index 8fe930ed9..e6ca38c67 100644 --- a/anki/models.py +++ b/anki/models.py @@ -177,6 +177,9 @@ select id from cards where nid in (select id from notes where mid = ?)""", def have(self, id): return str(id) in self.models + def ids(self): + return self.models.keys() + # Tools ##################################################