remove did from notes, use model did instead

This commit is contained in:
Damien Elmes 2012-03-09 09:12:27 +09:00
parent ffaf7ffc66
commit f6b2e69669
13 changed files with 44 additions and 47 deletions

View file

@ -292,10 +292,11 @@ crt=?, mod=?, scm=?, dty=?, usn=?, ls=?, conf=?""",
now = intTime() now = intTime()
rem = [] rem = []
usn = self.usn() usn = self.usn()
for nid, mid, did, flds in self.db.execute( for nid, mid, flds in self.db.execute(
"select id, mid, did, flds from notes where id in "+snids): "select id, mid, flds from notes where id in "+snids):
model = self.models.get(mid) model = self.models.get(mid)
avail = self.models.availOrds(model, flds) avail = self.models.availOrds(model, flds)
did = model['did']
for t in model['tmpls']: for t in model['tmpls']:
doHave = nid in have and t['ord'] in have[nid] doHave = nid in have and t['ord'] in have[nid]
# if have ord but empty, add cid to remove list # if have ord but empty, add cid to remove list
@ -335,7 +336,7 @@ insert into cards values (?,?,?,?,?,?,0,0,?,0,0,0,0,0,0,0,0,"")""",
card = anki.cards.Card(self) card = anki.cards.Card(self)
card.nid = note.id card.nid = note.id
card.ord = template['ord'] card.ord = template['ord']
card.did = template['did'] or note.did card.did = template['did'] or note.model()['did']
card.due = self._dueForDid(card.did, due) card.due = self._dueForDid(card.did, due)
if flush: if flush:
card.flush() card.flush()

View file

@ -32,7 +32,7 @@ MEDIA_ADD = 0
MEDIA_REM = 1 MEDIA_REM = 1
# deck schema & syncing vars # deck schema & syncing vars
SCHEMA_VERSION = 2 SCHEMA_VERSION = 3
SYNC_ZIP_SIZE = int(2.5*1024*1024) SYNC_ZIP_SIZE = int(2.5*1024*1024)
SYNC_URL = os.environ.get("SYNC_URL") or "https://beta.ankiweb.net/sync/" SYNC_URL = os.environ.get("SYNC_URL") or "https://beta.ankiweb.net/sync/"
SYNC_VER = 1 SYNC_VER = 1

View file

@ -147,7 +147,7 @@ class AnkiExporter(Exporter):
notedata = self.src.db.all("select * from notes where id in "+ notedata = self.src.db.all("select * from notes where id in "+
strnids) strnids)
self.dst.db.executemany( self.dst.db.executemany(
"insert into notes values (?,?,?,?,?,?,?,?,?,?,?,?)", "insert into notes values (?,?,?,?,?,?,?,?,?,?,?)",
notedata) notedata)
# models used by the notes # models used by the notes
mids = self.dst.db.list("select distinct mid from notes where id in "+ mids = self.dst.db.list("select distinct mid from notes where id in "+

View file

@ -83,19 +83,18 @@ class Anki2Importer(Importer):
existing[note[0]] = True existing[note[0]] = True
# rewrite internal ids, models, etc # rewrite internal ids, models, etc
note[2] = lmid note[2] = lmid
note[3] = self._did(note[3]) note[3] = intTime()
note[4] = intTime() note[4] = usn
note[5] = usn
add.append(note) add.append(note)
dirty.append(note[0]) dirty.append(note[0])
# note we have the added note # note we have the added note
self._notes[guid] = (note[0], note[4], note[2]) self._notes[guid] = (note[0], note[3], note[2])
else: else:
# not yet implemented # not yet implemented
pass pass
# add to col # add to col
self.dst.db.executemany( self.dst.db.executemany(
"insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?,?)", "insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?)",
add) add)
self.dst.updateFieldCache(dirty) self.dst.updateFieldCache(dirty)
self.dst.tags.registerNotes(dirty) self.dst.tags.registerNotes(dirty)

View file

@ -150,13 +150,13 @@ class NoteImporter(Importer):
for ord, c in n.cards.items(): for ord, c in n.cards.items():
self._cards.append((id, ord, c)) self._cards.append((id, ord, c))
self.col.tags.register(n.tags) self.col.tags.register(n.tags)
return [id, guid64(), self.model['id'], self.didForNote(n), return [id, guid64(), self.model['id'],
intTime(), self.col.usn(), self.col.tags.join(n.tags), intTime(), self.col.usn(), self.col.tags.join(n.tags),
n.fieldsStr, "", "", 0, ""] n.fieldsStr, "", "", 0, ""]
def addNew(self, rows): def addNew(self, rows):
self.col.db.executemany( self.col.db.executemany(
"insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?,?)", "insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?)",
rows) rows)
# need to document that deck is ignored in this case # need to document that deck is ignored in this case
@ -175,13 +175,6 @@ class NoteImporter(Importer):
update notes set mod = ?, usn = ?, flds = ?, tags = ? update notes set mod = ?, usn = ?, flds = ?, tags = ?
where id = ? and (flds != ? or tags != ?)""", rows) where id = ? and (flds != ? or tags != ?)""", rows)
def didForNote(self, n):
if not n.deck:
n.deck = _("Imported")
if n.deck not in self._deckMap:
self._deckMap[n.deck] = self.col.decks.id(n.deck)
return self._deckMap[n.deck]
def processFields(self, note): def processFields(self, note):
fields = [""]*len(self.model['flds']) fields = [""]*len(self.model['flds'])
for c, f in enumerate(self.mapping): for c, f in enumerate(self.mapping):

View file

@ -19,7 +19,6 @@ class Note(object):
self.id = timestampID(col.db, "notes") self.id = timestampID(col.db, "notes")
self.guid = guid64() self.guid = guid64()
self._model = model self._model = model
self.did = model['did']
self.mid = model['id'] self.mid = model['id']
self.tags = [] self.tags = []
self.fields = [""] * len(self._model['flds']) self.fields = [""] * len(self._model['flds'])
@ -31,14 +30,13 @@ class Note(object):
def load(self): def load(self):
(self.guid, (self.guid,
self.mid, self.mid,
self.did,
self.mod, self.mod,
self.usn, self.usn,
self.tags, self.tags,
self.fields, self.fields,
self.flags, self.flags,
self.data) = self.col.db.first(""" self.data) = self.col.db.first("""
select guid, mid, did, mod, usn, tags, flds, flags, data select guid, mid, mod, usn, tags, flds, flags, data
from notes where id = ?""", self.id) from notes where id = ?""", self.id)
self.fields = splitFields(self.fields) self.fields = splitFields(self.fields)
self.tags = self.col.tags.split(self.tags) self.tags = self.col.tags.split(self.tags)
@ -55,8 +53,8 @@ from notes where id = ?""", self.id)
tags = self.stringTags() tags = self.stringTags()
csum = fieldChecksum(self.fields[0]) csum = fieldChecksum(self.fields[0])
res = self.col.db.execute(""" res = self.col.db.execute("""
insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?,?)""", insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?)""",
self.id, self.guid, self.mid, self.did, self.id, self.guid, self.mid,
self.mod, self.usn, tags, self.mod, self.usn, tags,
self.joinedFields(), sfld, csum, self.flags, self.joinedFields(), sfld, csum, self.flags,
self.data) self.data)
@ -73,12 +71,6 @@ insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?,?)""",
def model(self): def model(self):
return self._model return self._model
def updateCardDids(self):
for c in self.cards():
if c.did != self.did and not c.template()['did']:
c.did = self.did
c.flush()
# Dict interface # Dict interface
################################################## ##################################################

View file

@ -50,8 +50,7 @@ class CardStats(object):
self.addLine(_("Position"), c.due) self.addLine(_("Position"), c.due)
self.addLine(_("Card Type"), c.template()['name']) self.addLine(_("Card Type"), c.template()['name'])
self.addLine(_("Note Type"), c.model()['name']) self.addLine(_("Note Type"), c.model()['name'])
self.addLine(_("Card Deck"), self.col.decks.name(c.did)) self.addLine(_("Deck"), self.col.decks.name(c.did))
self.addLine(_("Note Deck"), self.col.decks.name(c.note().did))
self.txt += "</table>" self.txt += "</table>"
return self.txt return self.txt

View file

@ -46,8 +46,11 @@ def Collection(path, lock=True, server=False, sync=True):
# no upgrades necessary at the moment # no upgrades necessary at the moment
def _upgradeSchema(db): def _upgradeSchema(db):
if db.scalar("select ver from col") < SCHEMA_VERSION: if db.scalar("select ver from col") == SCHEMA_VERSION:
print "upgrading" return SCHEMA_VERSION
# add odid to cards, edue->odue
######################################################################
if db.scalar("select ver from col") == 1:
db.execute("alter table cards rename to cards2") db.execute("alter table cards rename to cards2")
_addSchema(db) _addSchema(db)
db.execute(""" db.execute("""
@ -57,6 +60,17 @@ left, edue, 0, flags, data from cards2""")
db.execute("drop table cards2") db.execute("drop table cards2")
db.execute("update col set ver = 2") db.execute("update col set ver = 2")
_updateIndices(db) _updateIndices(db)
# remove did from notes
######################################################################
if db.scalar("select ver from col") == 2:
db.execute("alter table notes rename to notes2")
_addSchema(db)
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 SCHEMA_VERSION return SCHEMA_VERSION
def _upgrade(col, ver): def _upgrade(col, ver):
@ -96,7 +110,6 @@ create table if not exists notes (
id integer primary key, id integer primary key,
guid text not null, guid text not null,
mid integer not null, mid integer not null,
did integer not null,
mod integer not null, mod integer not null,
usn integer not null, usn integer not null,
tags text not null, tags text not null,

View file

@ -185,7 +185,7 @@ select id, nid, did, ord, mod, %d, type, queue, due, ivl, factor, reps,
lapses, left, odue, odid, flags, data from cards where %s""" % d) lapses, left, odue, odid, flags, data from cards where %s""" % d)
else: else:
return x(""" return x("""
select id, guid, mid, did, mod, %d, tags, flds, '', '', flags, data select id, guid, mid, mod, %d, tags, flds, '', '', flags, data
from notes where %s""" % d) from notes where %s""" % d)
def chunk(self): def chunk(self):
@ -369,7 +369,7 @@ from notes where %s""" % d)
def mergeNotes(self, notes): def mergeNotes(self, notes):
rows = self.newerRows(notes, "notes", 4) rows = self.newerRows(notes, "notes", 4)
self.col.db.executemany( self.col.db.executemany(
"insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?,?)", "insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?)",
rows) rows)
self.col.updateFieldCache([f[0] for f in rows]) self.col.updateFieldCache([f[0] for f in rows])

View file

@ -149,7 +149,7 @@ end)
""") """)
# pull facts into memory, so we can merge them with fields efficiently # pull facts into memory, so we can merge them with fields efficiently
facts = db.all(""" facts = db.all("""
select id, id, modelId, 1, cast(created*1000 as int), cast(modified as int), select id, id, modelId, cast(created*1000 as int), cast(modified as int),
0, tags from facts order by created""") 0, tags from facts order by created""")
# build field hash # build field hash
fields = {} fields = {}
@ -185,7 +185,7 @@ select id, id, modelId, 1, cast(created*1000 as int), cast(modified as int),
# and put the facts into the new table # and put the facts into the new table
db.execute("drop table facts") db.execute("drop table facts")
_addSchema(db, False) _addSchema(db, False)
db.executemany("insert into notes values (?,?,?,?,?,?,?,?,'','',0,'')", data) db.executemany("insert into notes values (?,?,?,?,?,?,?,'','',0,'')", data)
db.execute("drop table fields") db.execute("drop table fields")
# cards # cards

View file

@ -39,7 +39,7 @@ def test_remove():
g1 = deck.decks.id("g1") g1 = deck.decks.id("g1")
f = deck.newNote() f = deck.newNote()
f['Front'] = u"1" f['Front'] = u"1"
f.did = g1 f.model()['did'] = g1
deck.addNote(f) deck.addNote(f)
c = f.cards()[0] c = f.cards()[0]
assert c.did == g1 assert c.did == g1

View file

@ -20,7 +20,7 @@ def setup1():
# with a different deck # with a different deck
f = deck.newNote() f = deck.newNote()
f['Front'] = u"baz"; f['Back'] = u"qux" f['Front'] = u"baz"; f['Back'] = u"qux"
f.did = deck.decks.id("new deck") f.model()['did'] = deck.decks.id("new deck")
deck.addNote(f) deck.addNote(f)
########################################################################## ##########################################################################

View file

@ -61,7 +61,7 @@ def test_newLimits():
f = d.newNote() f = d.newNote()
f['Front'] = str(i) f['Front'] = str(i)
if i > 4: if i > 4:
f.did = g2 f.model()['did'] = g2
d.addNote(f) d.addNote(f)
# give the child deck a different configuration # give the child deck a different configuration
c2 = d.decks.confId("new conf") c2 = d.decks.confId("new conf")
@ -716,7 +716,7 @@ def test_deckDue():
# and one that's a child # and one that's a child
f = d.newNote() f = d.newNote()
f['Front'] = u"two" f['Front'] = u"two"
default1 = f.did = d.decks.id("Default::1") default1 = f.model()['did'] = d.decks.id("Default::1")
d.addNote(f) d.addNote(f)
# make it a review card # make it a review card
c = f.cards()[0] c = f.cards()[0]
@ -726,12 +726,12 @@ def test_deckDue():
# add one more with a new deck # add one more with a new deck
f = d.newNote() f = d.newNote()
f['Front'] = u"two" f['Front'] = u"two"
foobar = f.did = d.decks.id("foo::bar") foobar = f.model()['did'] = d.decks.id("foo::bar")
d.addNote(f) d.addNote(f)
# and one that's a sibling # and one that's a sibling
f = d.newNote() f = d.newNote()
f['Front'] = u"three" f['Front'] = u"three"
foobaz = f.did = d.decks.id("foo::baz") foobaz = f.model()['did'] = d.decks.id("foo::baz")
d.addNote(f) d.addNote(f)
d.reset() d.reset()
assert len(d.decks.decks) == 5 assert len(d.decks.decks) == 5
@ -775,12 +775,12 @@ def test_deckFlow():
# and one that's a child # and one that's a child
f = d.newNote() f = d.newNote()
f['Front'] = u"two" f['Front'] = u"two"
default1 = f.did = d.decks.id("Default::2") default1 = f.model()['did'] = d.decks.id("Default::2")
d.addNote(f) d.addNote(f)
# and another that's higher up # and another that's higher up
f = d.newNote() f = d.newNote()
f['Front'] = u"three" f['Front'] = u"three"
default1 = f.did = d.decks.id("Default::1") default1 = f.model()['did'] = d.decks.id("Default::1")
d.addNote(f) d.addNote(f)
# should get top level one first, then ::1, then ::2 # should get top level one first, then ::1, then ::2
d.reset() d.reset()