diff --git a/anki/collection.py b/anki/collection.py index a8aa86eb9..248c4a3e1 100644 --- a/anki/collection.py +++ b/anki/collection.py @@ -201,6 +201,7 @@ crt=?, mod=?, scm=?, dty=?, usn=?, ls=?, conf=?""", self.modSchema(check=False) self.ls = self.scm # ensure db is compacted before upload + self.db.setAutocommit(True) self.db.execute("vacuum") self.db.execute("analyze") self.close() @@ -810,8 +811,10 @@ and queue = 0""", intTime(), self.usn()) return ("\n".join(problems), ok) def optimize(self): + self.db.setAutocommit(True) self.db.execute("vacuum") self.db.execute("analyze") + self.db.setAutocommit(False) self.lock() # Logging diff --git a/anki/db.py b/anki/db.py index f9fe4a4d9..f66a590fe 100644 --- a/anki/db.py +++ b/anki/db.py @@ -5,14 +5,7 @@ import os import time -try: - from pysqlite2 import dbapi2 as sqlite - vi = sqlite.version_info - if vi[0] > 2 or vi[1] > 6: - # latest pysqlite breaks anki - raise ImportError() -except ImportError: - from sqlite3 import dbapi2 as sqlite +from sqlite3 import dbapi2 as sqlite Error = sqlite.Error @@ -103,3 +96,9 @@ class DB(object): def interrupt(self): self._db.interrupt() + + def setAutocommit(self, autocommit): + if autocommit: + self._db.isolation_level = None + else: + self._db.isolation_level = '' diff --git a/anki/importing/anki2.py b/anki/importing/anki2.py index abd19eb0f..070c13d43 100644 --- a/anki/importing/anki2.py +++ b/anki/importing/anki2.py @@ -46,8 +46,10 @@ class Anki2Importer(Importer): self._importCards() self._importStaticMedia() self._postImport() + self.dst.db.setAutocommit(True) self.dst.db.execute("vacuum") self.dst.db.execute("analyze") + self.dst.db.setAutocommit(False) # Notes ###################################################################### diff --git a/anki/media.py b/anki/media.py index e1bfa09e5..88b5333c8 100644 --- a/anki/media.py +++ b/anki/media.py @@ -444,8 +444,10 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0); def forceResync(self): self.db.execute("delete from media") self.db.execute("update meta set lastUsn=0,dirMod=0") - self.db.execute("vacuum analyze") self.db.commit() + self.db.setAutocommit(True) + self.db.execute("vacuum analyze") + self.db.setAutocommit(False) # Media syncing: zips ########################################################################## diff --git a/anki/storage.py b/anki/storage.py index e209c51ff..fe2006846 100644 --- a/anki/storage.py +++ b/anki/storage.py @@ -2,7 +2,6 @@ # Copyright: Damien Elmes # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -import os import copy import re @@ -14,7 +13,6 @@ from anki.consts import * from anki.stdmodels import addBasicModel, addClozeModel, addForwardReverse, \ addForwardOptionalReverse - def Collection(path, lock=True, server=False, sync=True, log=False): "Open a new or existing collection. Path must be unicode." assert path.endswith(".anki2") @@ -26,6 +24,7 @@ def Collection(path, lock=True, server=False, sync=True, log=False): assert c not in base # connect db = DB(path) + db.setAutocommit(True) if create: ver = _createDB(db) else: @@ -36,6 +35,7 @@ def Collection(path, lock=True, server=False, sync=True, log=False): db.execute("pragma journal_mode = wal") else: db.execute("pragma synchronous = off") + db.setAutocommit(False) # add db to col and do any remaining upgrades col = _Collection(db, server, log) if ver < SCHEMA_VERSION: