From f59a53ab2244af855d1bd8b78bb3d0aed3542e74 Mon Sep 17 00:00:00 2001 From: dobefore <46564102+dobefore@users.noreply.github.com> Date: Sat, 1 May 2021 12:51:05 +0800 Subject: [PATCH] Update sync.py --- src/ankisyncd/sync.py | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/ankisyncd/sync.py b/src/ankisyncd/sync.py index f50cbd5..6e2b7fb 100644 --- a/src/ankisyncd/sync.py +++ b/src/ankisyncd/sync.py @@ -72,9 +72,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( @@ -568,7 +599,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()