コード例 #1
0
ファイル: collection.py プロジェクト: ytsheng/anki
 def modSchema(self, check):
     "Mark schema modified. Call this first so user can abort if necessary."
     if not self.schemaChanged():
         if check and not runFilter("modSchema", True):
             raise AnkiError("abortSchemaMod")
     self.scm = intTime(1000)
     self.setMod()
コード例 #2
0
ファイル: collection.py プロジェクト: halhenke/anki
 def modSchema(self, check: bool) -> None:
     "Mark schema modified. Call this first so user can abort if necessary."
     if not self.schemaChanged():
         if check and not hooks.schema_will_change(proceed=True):
             raise AnkiError("abortSchemaMod")
     self.scm = intTime(1000)
     self.setMod()
コード例 #3
0
ファイル: deck.py プロジェクト: ChYi/libanki
 def modSchema(self, check=True):
     "Mark schema modified. Call this first so user can abort if necessary."
     if not self.schemaChanged():
         if check and not runFilter("modSchema", True):
             raise AnkiError("abortSchemaMod")
         # next sync will be full
         self.emptyTrash()
     self.scm = intTime()
コード例 #4
0
ファイル: collection.py プロジェクト: swearofwind/anki
    def modSchema(self, check):
        """Mark schema modified. Call this first so user can abort if necessary.

        Raise AnkiError("abortSchemaMod") if the change is
        rejected by the filter (e.g. if the user states to abort).

        Once the change is accepted, the filter is not run until a
        synchronization occurs.

        Change the scm value
        """
        if not self.schemaChanged():
            if check and not runFilter("modSchema", True):
                raise AnkiError("abortSchemaMod")
        self.scm = intTime(1000)
        self.setMod()
コード例 #5
0
ファイル: storage.py プロジェクト: ChYi/libanki
def _upgradeSchema(db):
    "Alter tables prior to ORM initialization."
    try:
        ver = db.scalar("select ver from deck")
    except:
        ver = db.scalar("select version from decks")
    # latest 1.2 is 65
    if ver < 65:
        raise AnkiError("oldDeckVersion")
    if ver > 99:
        return ver
    runHook("1.x upgrade", db)

    # cards
    ###########
    # move into temp table
    _moveTable(db, "cards", True)
    # use the new order to rewrite card ids
    cardmap = dict(db.all("select id, rowid from cards2"))
    # move back, preserving new ids, and rewriting types
    db.execute("""
insert into cards select rowid, factId, 1, ordinal, cast(created as int),
cast(modified as int),
(case relativeDelay
when 0 then 1
when 1 then 2
when 2 then 0 end),
(case type
when 0 then 1
when 1 then 2
when 2 then 0
else type end),
cast(due as int), cast(interval as int),
cast(factor*1000 as int), reps, noCount, 0, 0, 0, "" from cards2
order by created""")
    db.execute("drop table cards2")

    # tags
    ###########
    _moveTable(db, "tags")
    db.execute("insert or ignore into tags select id, ?, tag from tags2",
               intTime())
    db.execute("drop table tags2")
    db.execute("drop table cardTags")

    # facts
    ###########
    # tags should have a leading and trailing space if not empty, and not
    # use commas
    db.execute("""
update facts set tags = (case
when trim(tags) == "" then ""
else " " || replace(replace(trim(tags), ",", " "), "  ", " ") || " "
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
from facts order by created""")
    # build field hash
    fields = {}
    for (fid, ord, val) in db.execute(
        "select factId, ordinal, value from fields order by factId, ordinal"):
        if fid not in fields:
            fields[fid] = []
        fields[fid].append((ord, val))
    # build insert data and transform ids, and minimize qt's
    # bold/italics/underline cruft.
    map = {}
    data = []
    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
        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
    _insertWithIdChange(db, map, 1, "cards", 17)
    # and put the facts into the new table
    db.execute("drop table facts")
    _addSchema(db, False)
    db.executemany("insert into facts values (?,?,?,?,?,?,?,'','')", data)
    db.execute("drop table fields")

    # media
    ###########
    db.execute("drop table media")

    # models
    ###########
    import anki.models
    _moveTable(db, "models")
    db.execute("""
insert into models select id, cast(created as int), cast(modified as int),
name, "{}", "{}", ?, "" from models2""", simplejson.dumps(
    anki.models.defaultConf))
    db.execute("drop table models2")

    # reviewHistory -> revlog
    ###########
    # fetch the data so we can rewrite ids quickly
    r = []
    for row in db.execute("""
select
cast(time*1000 as int), cardId, ease,
cast(nextInterval as int), cast(lastInterval as int),
cast(nextFactor*1000 as int), cast(min(thinkingTime, 60)*1000 as int),
yesCount from reviewHistory"""):
        row = list(row)
        # new card ids
        try:
            row[1] = cardmap[row[1]]
        except:
            # id doesn't exist
            continue
        # no ease 0 anymore
        row[2] = row[2] or 1
        # determine type, overwriting yesCount
        newInt = row[3]
        oldInt = row[4]
        yesCnt = row[7]
        # yesCnt included the current answer
        if row[2] > 1:
            yesCnt -= 1
        if oldInt < 1:
            # new or failed
            if yesCnt:
                # type=relrn
                row[7] = 2
            else:
                # type=lrn
                row[7] = 0
        else:
            # type=rev
            row[7] = 1
        r.append(row)
    db.executemany(
        "insert or ignore into revlog values (?,?,?,?,?,?,?,?)", r)
    db.execute("drop table reviewHistory")

    # longer migrations
    ###########
    _migrateDeckTbl(db)
    mods = _migrateFieldsTbl(db)
    _migrateTemplatesTbl(db, mods)

    _updateIndices(db)
    return ver