fix upgrade

This commit is contained in:
Damien Elmes 2011-08-28 00:20:24 +09:00
parent 78600e8ed6
commit d20984a686
2 changed files with 52 additions and 75 deletions

View file

@ -689,7 +689,7 @@ select id from facts where id not in (select distinct fid from cards)""")
self.updateFactTags() self.updateFactTags()
# field cache # field cache
for m in self.models.all(): for m in self.models.all():
self.updateFieldCache(self.models.fids(m['id'])) self.updateFieldCache(self.models.fids(m))
# and finally, optimize # and finally, optimize
self.optimize() self.optimize()
newSize = os.stat(self.path)[stat.ST_SIZE] newSize = os.stat(self.path)[stat.ST_SIZE]

View file

@ -286,20 +286,6 @@ order by created"""):
insert into cards values (?, ?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, 0, 0, "")""", insert into cards values (?, ?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, 0, 0, "")""",
rows) rows)
# media
###########
db.execute("drop table media")
# models
###########
import anki.models
_moveTable(db, "models")
db.execute("""
insert into models select id, cast(created*1000 as int),
name, "{}", "{}", ?, "" from models2""", simplejson.dumps(
anki.models.defaultConf))
db.execute("drop table models2")
# reviewHistory -> revlog # reviewHistory -> revlog
########### ###########
# fetch the data so we can rewrite ids quickly # fetch the data so we can rewrite ids quickly
@ -342,12 +328,11 @@ yesCount from reviewHistory"""):
"insert or ignore into revlog values (?,?,?,?,?,?,?,?)", r) "insert or ignore into revlog values (?,?,?,?,?,?,?,?)", r)
db.execute("drop table reviewHistory") db.execute("drop table reviewHistory")
# longer migrations # the rest
########### ###########
db.execute("drop table media")
_migrateDeckTbl(db) _migrateDeckTbl(db)
mods = _migrateFieldsTbl(db) _migrateModels(db)
_migrateTemplatesTbl(db, mods)
_updateIndices(db) _updateIndices(db)
return ver return ver
@ -389,27 +374,51 @@ insert or replace into deck select id, cast(created as int), :t,
else: else:
conf[k] = v conf[k] = v
import anki.groups import anki.groups
db.execute("update deck set qconf = :l,conf = :c,data = :d,groups=:g,gconf=:gc", db.execute("update deck set qconf = :l,conf = :c,groups=:g,gconf=:gc",
l=simplejson.dumps(qconf), l=simplejson.dumps(qconf),
c=simplejson.dumps(conf), c=simplejson.dumps(conf),
d=simplejson.dumps(data),
g=simplejson.dumps({'1': {'name': _("Default"), 'conf': 1}}), g=simplejson.dumps({'1': {'name': _("Default"), 'conf': 1}}),
gc=simplejson.dumps({'1': anki.groups.defaultConf})) gc=simplejson.dumps({'1': anki.groups.defaultConf}))
# clean up # clean up
db.execute("drop table decks") db.execute("drop table decks")
db.execute("drop table deckVars") db.execute("drop table deckVars")
def _migrateFieldsTbl(db): def _migrateModels(db):
import anki.models
times = {}
mods = {}
for row in db.all(
"select id, name from models"):
while 1:
t = intTime(1000)
if t not in times:
times[t] = True
break
m = anki.models.defaultModel.copy()
m['id'] = t
m['name'] = row[1]
m['mod'] = intTime()
m['tags'] = []
m['flds'] = _fieldsForModel(db, row[0])
m['tmpls'] = _templatesForModel(db, row[0], m['flds'])
mods[m['id']] = m
db.execute("update facts set mid = ? where mid = ?", t, row[0])
# save and clean up
db.execute("update deck set models = ?", simplejson.dumps(mods))
db.execute("drop table fieldModels")
db.execute("drop table cardModels")
db.execute("drop table models")
def _fieldsForModel(db, mid):
import anki.models import anki.models
dconf = anki.models.defaultField dconf = anki.models.defaultField
mods = {} flds = []
for row in db.all(""" for c, row in enumerate(db.all("""
select modelId, name, features, required, "unique", select name, features, required, "unique",
quizFontFamily, quizFontSize, quizFontColour, editFontSize from fieldModels quizFontFamily, quizFontSize, quizFontColour, editFontSize from fieldModels
order by modelId, ordinal"""): where modelId = ?
order by ordinal""", mid)):
conf = dconf.copy() conf = dconf.copy()
if row[0] not in mods:
mods[row[0]] = []
(conf['name'], (conf['name'],
conf['rtl'], conf['rtl'],
conf['req'], conf['req'],
@ -417,7 +426,8 @@ order by modelId, ordinal"""):
conf['font'], conf['font'],
conf['qsize'], conf['qsize'],
conf['qcol'], conf['qcol'],
conf['esize']) = row[1:] conf['esize']) = row
conf['ord'] = c
# ensure data is good # ensure data is good
conf['rtl'] = not not conf['rtl'] conf['rtl'] = not not conf['rtl']
conf['pre'] = True conf['pre'] = True
@ -425,26 +435,19 @@ order by modelId, ordinal"""):
conf['qcol'] = conf['qcol'] or "#000" conf['qcol'] = conf['qcol'] or "#000"
conf['qsize'] = conf['qsize'] or 20 conf['qsize'] = conf['qsize'] or 20
conf['esize'] = conf['esize'] or 20 conf['esize'] = conf['esize'] or 20
mods[row[0]].append(conf) flds.append(conf)
# now we've gathered all the info, save it into the models return flds
for mid, fms in mods.items():
db.execute("update models set flds = ? where id = ?",
simplejson.dumps(fms), mid)
# clean up
db.execute("drop table fieldModels")
return mods
def _migrateTemplatesTbl(db, fmods): def _templatesForModel(db, mid, flds):
import anki.models import anki.models
dconf = anki.models.defaultTemplate dconf = anki.models.defaultTemplate
mods = {} tmpls = []
for row in db.all(""" for c, row in enumerate(db.all("""
select modelId, name, active, qformat, aformat, questionInAnswer, select name, active, qformat, aformat, questionInAnswer,
questionAlign, lastFontColour, allowEmptyAnswer, typeAnswer from cardModels questionAlign, lastFontColour, allowEmptyAnswer, typeAnswer from cardModels
order by modelId, ordinal"""): where modelId = ?
order by ordinal""", mid)):
conf = dconf.copy() conf = dconf.copy()
if row[0] not in mods:
mods[row[0]] = []
(conf['name'], (conf['name'],
conf['actv'], conf['actv'],
conf['qfmt'], conf['qfmt'],
@ -453,10 +456,11 @@ order by modelId, ordinal"""):
conf['align'], conf['align'],
conf['bg'], conf['bg'],
conf['emptyAns'], conf['emptyAns'],
conf['typeAns']) = row[1:] conf['typeAns']) = row
conf['ord'] = c
# convert the field name to an ordinal # convert the field name to an ordinal
ordN = None ordN = None
for (ord, fm) in enumerate(fmods[row[0]]): for (ord, fm) in enumerate(flds):
if fm['name'] == conf['typeAns']: if fm['name'] == conf['typeAns']:
ordN = ord ordN = ord
break break
@ -474,39 +478,12 @@ order by modelId, ordinal"""):
"(?i){{cardModel}}", "{{Template}}", conf[type]) "(?i){{cardModel}}", "{{Template}}", conf[type])
conf[type] = re.sub( conf[type] = re.sub(
"(?i){{modelTags}}", "{{Model}}", conf[type]) "(?i){{modelTags}}", "{{Model}}", conf[type])
mods[row[0]].append(conf) tmpls.append(conf)
# now we've gathered all the info, save it into the models return tmpls
for mid, tmpls in mods.items():
db.execute("update models set tmpls = ? where id = ?",
simplejson.dumps(tmpls), mid)
# clean up
db.execute("drop table cardModels")
return mods
def _fixupModels(deck):
# rewrite model/template/field ids
models = deck.models.all()
deck.db.execute("delete from models")
times = {}
for c, m in enumerate(models.values()):
# update ordinals
m._updateFieldOrds()
m._updateTemplOrds()
# we've temporarily stored the model creation time in the mod time
old = m.id
while m.mod in times:
m.mod += 1
times[m.mod] = True
m.id = m.mod
m.mod = intTime()
m.flush()
deck.db.execute("update facts set mid = ? where mid = ?", m.id, old)
def _postSchemaUpgrade(deck): def _postSchemaUpgrade(deck):
"Handle the rest of the upgrade to 2.0." "Handle the rest of the upgrade to 2.0."
import anki.deck import anki.deck
# adjust models
_fixupModels(deck)
# fix creation time # fix creation time
deck.sched._updateCutoff() deck.sched._updateCutoff()
d = datetime.datetime.today() d = datetime.datetime.today()