drop the schema <11 upgrade code

This commit is contained in:
Damien Elmes 2020-03-03 07:47:17 +10:00
parent 527d269681
commit 5f442ae95e

View File

@ -4,7 +4,6 @@
import copy
import json
import os
import re
from typing import Any, Dict, Optional, Tuple
from anki.collection import _Collection
@ -58,7 +57,7 @@ def Collection(
# add db to col and do any remaining upgrades
col = _Collection(db, backend=backend, server=server, log=log)
if ver < SCHEMA_VERSION:
_upgrade(col, ver)
raise Exception("This file requires an older version of Anki.")
elif ver > SCHEMA_VERSION:
raise Exception("This file requires a newer version of Anki.")
elif create:
@ -79,159 +78,7 @@ def Collection(
def _upgradeSchema(db: DBProxy) -> Any:
ver = db.scalar("select ver from col")
if ver == SCHEMA_VERSION:
return ver
# add odid to cards, edue->odue
######################################################################
if db.scalar("select ver from col") == 1:
db.execute("alter table cards rename to cards2")
_addSchema(db, setColConf=False)
db.execute(
"""
insert into cards select
id, nid, did, ord, mod, usn, type, queue, due, ivl, factor, reps, lapses,
left, edue, 0, flags, data from cards2"""
)
db.execute("drop table cards2")
db.execute("update col set ver = 2")
_updateIndices(db)
# remove did from notes
######################################################################
if db.scalar("select ver from col") == 2:
db.execute("alter table notes rename to notes2")
_addSchema(db, setColConf=False)
db.execute(
"""
insert into notes select
id, guid, mid, mod, usn, tags, flds, sfld, csum, flags, data from notes2"""
)
db.execute("drop table notes2")
db.execute("update col set ver = 3")
_updateIndices(db)
return ver
def _upgrade(col, ver) -> None:
if ver < 3:
# new deck properties
for d in col.decks.all():
d["dyn"] = DECK_STD
d["collapsed"] = False
col.decks.save(d)
if ver < 4:
col.modSchema(check=False)
clozes = []
for m in col.models.all():
if not "{{cloze:" in m["tmpls"][0]["qfmt"]:
m["type"] = MODEL_STD
col.models.save(m)
else:
clozes.append(m)
for m in clozes:
_upgradeClozeModel(col, m)
col.db.execute("update col set ver = 4")
if ver < 5:
col.db.execute("update cards set odue = 0 where queue = 2")
col.db.execute("update col set ver = 5")
if ver < 6:
col.modSchema(check=False)
import anki.models
for m in col.models.all():
m["css"] = anki.models.defaultModel["css"]
for t in m["tmpls"]:
if "css" not in t:
# ankidroid didn't bump version
continue
m["css"] += "\n" + t["css"].replace(
".card ", ".card%d " % (t["ord"] + 1)
)
del t["css"]
col.models.save(m)
col.db.execute("update col set ver = 6")
if ver < 7:
col.modSchema(check=False)
col.db.execute(
"update cards set odue = 0 where (type = 1 or queue = 2) " "and not odid"
)
col.db.execute("update col set ver = 7")
if ver < 8:
col.modSchema(check=False)
col.db.execute("update cards set due = due / 1000 where due > 4294967296")
col.db.execute("update col set ver = 8")
if ver < 9:
# adding an empty file to a zip makes python's zip code think it's a
# folder, so remove any empty files
changed = False
dir = col.media.dir()
if dir:
for f in os.listdir(col.media.dir()):
if os.path.isfile(f) and not os.path.getsize(f):
os.unlink(f)
col.media.db.execute("delete from log where fname = ?", f)
col.media.db.execute("delete from media where fname = ?", f)
changed = True
if changed:
col.media.db.commit()
col.db.execute("update col set ver = 9")
if ver < 10:
col.db.execute(
"""
update cards set left = left + left*1000 where queue = 1"""
)
col.db.execute("update col set ver = 10")
if ver < 11:
col.modSchema(check=False)
for d in col.decks.all():
if d["dyn"]:
order = d["order"]
# failed order was removed
if order >= 5:
order -= 1
d["terms"] = [[d["search"], d["limit"], order]]
del d["search"]
del d["limit"]
del d["order"]
d["resched"] = True
d["return"] = True
else:
if "extendNew" not in d:
d["extendNew"] = 10
d["extendRev"] = 50
col.decks.save(d)
for c in col.decks.allConf():
r = c["rev"]
r["ivlFct"] = r.get("ivlfct", 1)
if "ivlfct" in r:
del r["ivlfct"]
r["maxIvl"] = 36500
col.decks.save(c)
for m in col.models.all():
for t in m["tmpls"]:
t["bqfmt"] = ""
t["bafmt"] = ""
col.models.save(m)
col.db.execute("update col set ver = 11")
def _upgradeClozeModel(col, m) -> None:
m["type"] = MODEL_CLOZE
# convert first template
t = m["tmpls"][0]
for type in "qfmt", "afmt":
t[type] = re.sub("{{cloze:1:(.+?)}}", r"{{cloze:\1}}", t[type])
t["name"] = _("Cloze")
# delete non-cloze cards for the model
rem = []
for t in m["tmpls"][1:]:
if "{{cloze:" not in t["qfmt"]:
rem.append(t)
for r in rem:
col.models.remTemplate(m, r)
del m["tmpls"][1:]
col.models._updateTemplOrds(m)
col.models.save(m)
return db.scalar("select ver from col")
# Creating a new collection