work around invalid 1.2 decks with multiple templates sharing same ord

This commit is contained in:
Damien Elmes 2012-10-24 13:11:18 +09:00
parent 7cec4762eb
commit b5c72155a3
3 changed files with 26 additions and 8 deletions

View file

@ -90,9 +90,6 @@ f.id = cards.factId)"""):
if db.list("""
select id from fields where factId not in (select id from facts)"""):
return
# fix ordinal numbers
db.execute("""update fields set ordinal = (select ordinal from
fieldModels where id = fieldModelId)""")
# incorrect types
if db.list("""
select id from cards where relativeDelay != (case
@ -129,11 +126,24 @@ fieldModels where id = fieldModelId)""")
db.execute("pragma page_size = 4096")
db.execute("pragma legacy_file_format = 0")
# previous versions sometimes had cards with incorrect ordinals and
# the db check didn't fix that. so we fix it here:
db.execute("""
update cards set ordinal = (select ordinal from cardModels
where id = cards.cardModelId)""")
for mid in db.list("select id from models"):
# ensure the ordinals are correct for each cardModel
for c, cmid in enumerate(db.list(
"select id from cardModels where modelId = ? order by ordinal",
mid)):
db.execute("update cardModels set ordinal = ? where id = ?",
c, cmid)
# and fieldModel
for c, fmid in enumerate(db.list(
"select id from fieldModels where modelId = ? order by ordinal",
mid)):
db.execute("update fieldModels set ordinal = ? where id = ?",
c, fmid)
# then fix ordinals numbers on cards & fields
db.execute("""update cards set ordinal = (select ordinal from
cardModels where cardModels.id = cardModelId)""")
db.execute("""update fields set ordinal = (select ordinal from
fieldModels where id = fieldModelId)""")
# notes
###########

Binary file not shown.

View file

@ -49,6 +49,14 @@ def test_upgrade1_due():
deck = u.upgrade(dst)
assert not deck.db.scalar("select 1 from cards where due != 1")
def test_invalid_ords():
dst = getUpgradeDeckPath("invalid-ords.anki")
u = Upgrader()
u.check(dst)
deck = u.upgrade(dst)
assert deck.db.scalar("select count() from cards where ord = 0") == 1
assert deck.db.scalar("select count() from cards where ord = 1") == 1
def test_upgrade2():
p = "/tmp/alpha-upgrade.anki2"
if os.path.exists(p):