diff --git a/src/ankisyncd/sync.py b/src/ankisyncd/sync.py index 9db4e76..99ce184 100644 --- a/src/ankisyncd/sync.py +++ b/src/ankisyncd/sync.py @@ -73,9 +73,40 @@ class Syncer(object): if 'crt' in rchg: self.col.crt = rchg['crt'] self.prepareToChunk() - +# this fn was cloned from anki module(version 2.1.36) + def basicCheck(self) -> bool: + "Basic integrity check for syncing. True if ok." + # cards without notes + if self.col.db.scalar( + """ +select 1 from cards where nid not in (select id from notes) limit 1""" + ): + return False + # notes without cards or models + if self.col.db.scalar( + """ +select 1 from notes where id not in (select distinct nid from cards) +or mid not in %s limit 1""" + % ids2str(self.col.models.ids()) + ): + return False + # invalid ords + for m in self.col.models.all(): + # ignore clozes + if m["type"] != MODEL_STD: + continue + if self.col.db.scalar( + """ +select 1 from cards where ord not in %s and nid in ( +select id from notes where mid = ?) limit 1""" + % ids2str([t["ord"] for t in m["tmpls"]]), + m["id"], + ): + return False + return True + def sanityCheck(self, full): - if not self.col.basicCheck(): + if not self.basicCheck(): return "failed basic check" for t in "cards", "notes", "revlog", "graves": if self.col.db.scalar( @@ -573,7 +604,7 @@ class FullSyncer(HttpSyncer): # make sure it's ok before we try to upload if self.col.db.scalar("pragma integrity_check") != "ok": return False - if not self.col.basicCheck(): + if not self.basicCheck(): return False # apply some adjustments, then upload self.col.beforeUpload()