From 644a885a07118db6f55e85f21571a671ca1c044b Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 26 Aug 2011 21:23:16 +0900 Subject: [PATCH] update fact ids, graves - should never skip recording graves, for the sake of merging - 1.0 upgrade will fail on decks that have the same fact creation date. need to work around this in the future --- anki/deck.py | 5 ----- anki/facts.py | 12 +++++------- anki/find.py | 2 +- anki/storage.py | 11 ++++++----- tests/test_cards.py | 13 +------------ 5 files changed, 13 insertions(+), 30 deletions(-) diff --git a/anki/deck.py b/anki/deck.py index e9d781cf6..579006e19 100644 --- a/anki/deck.py +++ b/anki/deck.py @@ -215,12 +215,7 @@ qconf=?, conf=?, data=?""", ########################################################################## def _logDels(self, ids, type): - # limit ids to those created prior to last sync tbl = "cards" if type == DEL_CARD else "facts" - ids = self.db.list( - "select id from %s where crt < ? and id in %s" % ( - tbl, ids2str(ids)), self.lastSync) - # log self.db.executemany("insert into graves values (%d, ?, %d)" % ( intTime(), type), ([x] for x in ids)) diff --git a/anki/facts.py b/anki/facts.py index 945305eb7..6ac4bfaa2 100644 --- a/anki/facts.py +++ b/anki/facts.py @@ -17,12 +17,11 @@ class Fact(object): self.id = id self.load() else: - self.id = None + self.id = intTime(1000) self._model = model self.gid = deck.defaultGroup(model.conf['gid']) self.mid = model.id - self.crt = intTime() - self.mod = self.crt + self.mod = intTime() self.tags = [] self.fields = [""] * len(self._model.fields) self.data = "" @@ -31,12 +30,11 @@ class Fact(object): def load(self): (self.mid, self.gid, - self.crt, self.mod, self.tags, self.fields, self.data) = self.deck.db.first(""" -select mid, gid, crt, mod, tags, flds, data from facts where id = ?""", self.id) +select mid, gid, mod, tags, flds, data from facts where id = ?""", self.id) self.fields = splitFields(self.fields) self.tags = parseTags(self.tags) self._model = self.deck.getModel(self.mid) @@ -47,8 +45,8 @@ select mid, gid, crt, mod, tags, flds, data from facts where id = ?""", self.id) sfld = stripHTML(self.fields[self._model.sortIdx()]) tags = self.stringTags() res = self.deck.db.execute(""" -insert or replace into facts values (?, ?, ?, ?, ?, ?, ?, ?, ?)""", - self.id, self.mid, self.gid, self.crt, +insert or replace into facts values (?, ?, ?, ?, ?, ?, ?, ?)""", + self.id, self.mid, self.gid, self.mod, tags, self.joinedFields(), sfld, self.data) self.id = res.lastrowid diff --git a/anki/find.py b/anki/find.py index 3891e2f6f..cfb074a42 100644 --- a/anki/find.py +++ b/anki/find.py @@ -69,7 +69,7 @@ class Finder(object): return "select id from cards c where " + lim elif type.startswith("fact"): if type == "factCrt": - sort = "f.crt, c.ord" + sort = "f.id, c.ord" elif type == "factMod": sort = "f.mod, c.ord" elif type == "factFld": diff --git a/anki/storage.py b/anki/storage.py index 22dd86079..920c80d7b 100644 --- a/anki/storage.py +++ b/anki/storage.py @@ -94,7 +94,6 @@ create table if not exists facts ( id integer primary key, mid integer not null, gid integer not null, - crt integer not null, mod integer not null, tags text not null, flds text not null, @@ -281,7 +280,7 @@ end) """) # pull facts into memory, so we can merge them with fields efficiently facts = db.all(""" -select id, modelId, 1, cast(created as int), cast(modified as int), tags +select id, modelId, 1, cast(created*1000 as int), cast(modified as int), tags from facts order by created""") # build field hash fields = {} @@ -297,9 +296,11 @@ from facts order by created""") from anki.utils import minimizeHTML for c, row in enumerate(facts): oldid = row[0] - map[oldid] = c+1 row = list(row) - row[0] = c+1 + # get rid of old created column and update id + row[0] = row[3] + del row[3] + map[oldid] = row[0] row.append(minimizeHTML("\x1f".join([x[1] for x in sorted(fields[oldid])]))) data.append(row) # use the new order to rewrite fact ids in cards table @@ -307,7 +308,7 @@ from facts order by created""") # and put the facts into the new table db.execute("drop table facts") _addSchema(db, False) - db.executemany("insert into facts values (?,?,?,?,?,?,?,'','')", data) + db.executemany("insert into facts values (?,?,?,?,?,?,'','')", data) db.execute("drop table fields") # media diff --git a/tests/test_cards.py b/tests/test_cards.py index 33a8e2996..92604e41b 100644 --- a/tests/test_cards.py +++ b/tests/test_cards.py @@ -65,18 +65,7 @@ def test_delete(): assert deck.db.scalar("select count() from cards") == 0 assert deck.db.scalar("select count() from fsums") == 0 assert deck.db.scalar("select count() from revlog") == 0 - assert deck.db.scalar("select count() from graves") == 0 - # add the fact back - deck.addFact(f) - assert deck.cardCount() == 1 - cid = f.cards()[0].id - # delete again, this time with syncing enabled - deck.syncName = "abc" - deck.lastSync = time.time() - deck.delCards([cid]) - assert deck.cardCount() == 0 - assert deck.factCount() == 0 - assert deck.db.scalar("select count() from graves") != 0 + assert deck.db.scalar("select count() from graves") == 2 def test_misc(): d = getEmptyDeck()