Skip to content

Commit

Permalink
fixes for sqlite on python 3.6
Browse files Browse the repository at this point in the history
we need to switch to autocommit mode before executing vacuums &
certain pragmas

also removed the pysqlite version workaround; we just use the
python-provided versions in all cases now as it is new enough
  • Loading branch information
dae committed Jan 13, 2017
1 parent a05ebdb commit e6adc3c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
3 changes: 3 additions & 0 deletions anki/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def beforeUpload(self):
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()
Expand Down Expand Up @@ -810,8 +811,10 @@ def fixIntegrity(self):
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
Expand Down
15 changes: 7 additions & 8 deletions anki/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -103,3 +96,9 @@ def totalChanges(self):

def interrupt(self):
self._db.interrupt()

def setAutocommit(self, autocommit):
if autocommit:
self._db.isolation_level = None
else:
self._db.isolation_level = ''
2 changes: 2 additions & 0 deletions anki/importing/anki2.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ def _import(self):
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
######################################################################
Expand Down
4 changes: 3 additions & 1 deletion anki/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,10 @@ def dirtyCount(self):
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
##########################################################################
Expand Down
4 changes: 2 additions & 2 deletions anki/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Copyright: Damien Elmes <anki@ichi2.net>
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html

import os
import copy
import re

Expand All @@ -14,7 +13,6 @@
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")
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit e6adc3c

Please sign in to comment.