Skip to content

Commit

Permalink
Merge pull request #87 from dobefore/add_basiccheck
Browse files Browse the repository at this point in the history
  • Loading branch information
VikashKothary committed Jun 2, 2021
2 parents 5687598 + f59a53a commit 43f4934
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/ankisyncd/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,40 @@ def mergeChanges(self, lchg, rchg):
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(
Expand Down Expand Up @@ -573,7 +604,7 @@ def upload(self):
# 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()
Expand Down

0 comments on commit 43f4934

Please sign in to comment.