From eba3aa583c465142901d7b6817a1ca824a4192d8 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sat, 28 Jun 2014 05:06:43 +0900 Subject: [PATCH] if media.db import fails, start anew --- anki/media.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/anki/media.py b/anki/media.py index 0fa4e32b6..0c9a57b42 100644 --- a/anki/media.py +++ b/anki/media.py @@ -3,6 +3,7 @@ # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import re +import traceback import urllib import unicodedata import sys @@ -77,21 +78,26 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0); def maybeUpgrade(self): oldpath = self.dir()+".db" if os.path.exists(oldpath): - self.db.execute('attach "../collection.media.db" as old') - self.db.execute(""" -insert into media - select m.fname, csum, mod, ifnull((select 1 from log l2 where l2.fname=m.fname), 0) as dirty - from old.media m - left outer join old.log l using (fname) - union - select fname, null, 0, 1 from old.log where type=1;""") - self.db.execute("delete from meta") - self.db.execute(""" -insert into meta select dirMod, usn from old.meta -""") - self.db.execute("detach old") - self.db.commit() - self.db.execute("vacuum analyze") + try: + self.db.execute('attach "../collection.media.db" as old') + self.db.execute(""" + insert into media + select m.fname, csum, mod, ifnull((select 1 from log l2 where l2.fname=m.fname), 0) as dirty + from old.media m + left outer join old.log l using (fname) + union + select fname, null, 0, 1 from old.log where type=1;""") + self.db.execute("delete from meta") + self.db.execute(""" + insert into meta select dirMod, usn from old.meta + """) + self.db.execute("detach old") + self.db.commit() + self.db.execute("vacuum analyze") + except Exception, e: + # if we couldn't import the old db for some reason, just start + # anew + self.col.log("failed to import old media db:"+traceback.format_exc()) os.rename("../collection.media.db", "../collection.media.db.old") def close(self):