From e21c944aeb2acae44396c575b6810ca18463b82d Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 7 Mar 2011 17:12:10 +0900 Subject: [PATCH] fix a few upgrade/cache issues - make sure we're actually stripping text in the field cache - make sure a default group is added on upgrade - make sure old style field references are upgrade --- anki/deck.py | 3 ++- anki/models.py | 2 -- anki/storage.py | 31 +++++++++++++++++-------------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/anki/deck.py b/anki/deck.py index dc4153c01..7d096a6eb 100644 --- a/anki/deck.py +++ b/anki/deck.py @@ -980,7 +980,8 @@ order by fdata.fid""" % ids2str(ids)), itemgetter(0)): "Add stripped HTML cache for searching." r = [] from anki.utils import stripHTMLMedia - [r.append((" ".join([x[1] for x in map.values()]), id)) + [r.append((stripHTMLMedia( + " ".join([x[1] for x in map.values()])), id)) for (id, map) in facts.items()] self.db.executemany( "update facts set cache=? where id=?", r) diff --git a/anki/models.py b/anki/models.py index 809c022f7..ebf61b4b7 100644 --- a/anki/models.py +++ b/anki/models.py @@ -152,8 +152,6 @@ insert or replace into fields values (?, ?, ?, ?, ?, ?)""", # Template object ########################################################################## -# FIXME: change typeAnswer to field id - defaultTemplateConf = { 'hideQ': False, 'align': 0, diff --git a/anki/storage.py b/anki/storage.py index af23ffb7d..e70383431 100644 --- a/anki/storage.py +++ b/anki/storage.py @@ -4,7 +4,7 @@ CURRENT_VERSION = 100 -import os, time, simplejson +import os, time, simplejson, re from anki.lang import _ from anki.utils import intTime from anki.db import DB @@ -45,7 +45,7 @@ def _createDB(db): db.execute("analyze") return CURRENT_VERSION -def _addSchema(db, addObjs=True): +def _addSchema(db, setDeckConf=True): db.executescript(""" create table if not exists deck ( id integer primary key, @@ -173,21 +173,20 @@ create table if not exists tags ( insert or ignore into deck values(1,%(t)s,%(t)s,%(t)s,%(v)s,'',0,-2,'', '', ''); """ % ({'t': intTime(), 'v':CURRENT_VERSION})) - # if not upgrading - if addObjs: - import anki.deck - import anki.groups + import anki.deck + import anki.groups + db.execute( + "insert or ignore into gconf values (1, ?, ?, ?)""", + intTime(), _("Default Config"), + simplejson.dumps(anki.groups.defaultConf)) + db.execute( + "insert or ignore into groups values (1, ?, ?, 1)", + intTime(), _("Default Group")) + if setDeckConf: db.execute("update deck set qconf = ?, conf = ?, data = ?", simplejson.dumps(anki.deck.defaultQconf), simplejson.dumps(anki.deck.defaultConf), "{}") - db.execute( - "insert or ignore into gconf values (1, ?, ?, ?)""", - intTime(), _("Default Config"), - simplejson.dumps(anki.groups.defaultConf)) - db.execute( - "insert or ignore into groups values (1, ?, ?, 1)", - intTime(), _("Default Group")) def _updateIndices(db): "Add indices to the DB." @@ -421,8 +420,12 @@ def _postSchemaUpgrade(deck): "revCardsDue", "revCardsRandom", "acqCardsRandom", "acqCardsOld", "acqCardsNew"): deck.db.execute("drop view if exists %s" % v) - # update caches + # ensure all templates use the new style field format, and update cach for m in deck.allModels(): + for t in m.templates: + t.qfmt = re.sub("%\((.+?)\)s", "{{\\1}}", t.qfmt) + t.afmt = re.sub("%\((.+?)\)s", "{{\\1}}", t.afmt) + m.flush() m.updateCache() # remove stats, as it's all in the revlog now deck.db.execute("drop table if exists stats")