catch corrupt media db, fix it in 'check media'

This commit is contained in:
Damien Elmes 2017-08-17 13:33:54 +10:00
parent e8cc960da3
commit 3ad9261c8c
4 changed files with 19 additions and 5 deletions

View file

@ -7,7 +7,7 @@ import time
from sqlite3 import dbapi2 as sqlite from sqlite3 import dbapi2 as sqlite
Error = sqlite.Error DBError = sqlite.Error
class DB: class DB:
def __init__(self, path, timeout=0): def __init__(self, path, timeout=0):

View file

@ -11,7 +11,7 @@ import zipfile
from io import StringIO from io import StringIO
from anki.utils import checksum, isWin, isMac, json from anki.utils import checksum, isWin, isMac, json
from anki.db import DB from anki.db import DB, DBError
from anki.consts import * from anki.consts import *
from anki.latex import mungeQA from anki.latex import mungeQA
@ -112,6 +112,12 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0);
# may have been deleted # may have been deleted
pass pass
def _deleteDB(self):
path = self.db._path
self.close()
os.unlink(path)
self.connect()
def dir(self): def dir(self):
return self._dir return self._dir
@ -290,6 +296,11 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0);
if renamedFiles: if renamedFiles:
return self.check(local=local) return self.check(local=local)
nohave = [x for x in allRefs if not x.startswith("_")] nohave = [x for x in allRefs if not x.startswith("_")]
# make sure the media DB is valid
try:
self.findChanges()
except DBError:
self._deleteDB()
return (nohave, unused, invalid) return (nohave, unused, invalid)
def _normalizeNoteRefs(self, nid): def _normalizeNoteRefs(self, nid):

View file

@ -7,7 +7,7 @@ import gzip
import random import random
import requests import requests
from anki.db import DB from anki.db import DB, DBError
from anki.utils import ids2str, intTime, json, platDesc, checksum, devMode from anki.utils import ids2str, intTime, json, platDesc, checksum, devMode
from anki.consts import * from anki.consts import *
from .hooks import runHook from .hooks import runHook
@ -701,7 +701,10 @@ class MediaSyncer:
# check if there have been any changes # check if there have been any changes
runHook("sync", "findMedia") runHook("sync", "findMedia")
self.col.log("findChanges") self.col.log("findChanges")
self.col.media.findChanges() try:
self.col.media.findChanges()
except DBError:
return "corruptMediaDB"
# begin session and check if in sync # begin session and check if in sync
lastUsn = self.col.media.lastUsn() lastUsn = self.col.media.lastUsn()

View file

@ -447,7 +447,7 @@ class SyncThread(QThread):
raise raise
if ret == "noChanges": if ret == "noChanges":
self.fireEvent("noMediaChanges") self.fireEvent("noMediaChanges")
elif ret == "sanityCheckFailed": elif ret == "sanityCheckFailed" or ret == "corruptMediaDB":
self.fireEvent("mediaSanity") self.fireEvent("mediaSanity")
else: else:
self.fireEvent("mediaSuccess") self.fireEvent("mediaSuccess")