Update sync.py

This commit is contained in:
dobefore 2021-05-01 12:51:05 +08:00 committed by GitHub
parent 811ae45d58
commit f59a53ab22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -72,9 +72,40 @@ class Syncer(object):
if 'crt' in rchg: if 'crt' in rchg:
self.col.crt = rchg['crt'] self.col.crt = rchg['crt']
self.prepareToChunk() 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): def sanityCheck(self, full):
if not self.col.basicCheck(): if not self.basicCheck():
return "failed basic check" return "failed basic check"
for t in "cards", "notes", "revlog", "graves": for t in "cards", "notes", "revlog", "graves":
if self.col.db.scalar( if self.col.db.scalar(
@ -568,7 +599,7 @@ class FullSyncer(HttpSyncer):
# make sure it's ok before we try to upload # make sure it's ok before we try to upload
if self.col.db.scalar("pragma integrity_check") != "ok": if self.col.db.scalar("pragma integrity_check") != "ok":
return False return False
if not self.col.basicCheck(): if not self.basicCheck():
return False return False
# apply some adjustments, then upload # apply some adjustments, then upload
self.col.beforeUpload() self.col.beforeUpload()