pypy: switch journal mode after upgrade (save it during the upgrade), to prevent errors like "database table is locked"

pull/1436/head
sebres 2016-05-20 13:26:51 +02:00
parent baafac36a4
commit 1718c8dbe9
1 changed files with 20 additions and 4 deletions

View File

@ -181,12 +181,24 @@ class Fail2BanDb(object):
filename, e.args[0]) filename, e.args[0])
raise raise
# differentiate pypy: switch journal mode later (save it during the upgrade),
# to prevent errors like "database table is locked":
try:
import __pypy__
pypy = True
except ImportError:
pypy = False
cur = self._db.cursor() cur = self._db.cursor()
cur.execute("PRAGMA foreign_keys = ON") cur.execute("PRAGMA foreign_keys = ON")
# speedup: write data through OS without syncing (no wait): # speedup: write data through OS without syncing (no wait):
cur.execute("PRAGMA synchronous = OFF") cur.execute("PRAGMA synchronous = OFF")
# speedup: transaction log in memory, alternate using OFF (disable, rollback will be impossible): # speedup: transaction log in memory, alternate using OFF (disable, rollback will be impossible):
cur.execute("PRAGMA journal_mode = MEMORY") if not pypy:
cur.execute("PRAGMA journal_mode = MEMORY")
# speedup: temporary tables and indices are kept in memory:
cur.execute("PRAGMA temp_store = MEMORY")
try: try:
cur.execute("SELECT version FROM fail2banDb LIMIT 1") cur.execute("SELECT version FROM fail2banDb LIMIT 1")
except sqlite3.OperationalError: except sqlite3.OperationalError:
@ -205,6 +217,9 @@ class Fail2BanDb(object):
Fail2BanDb.__version__, version, newversion) Fail2BanDb.__version__, version, newversion)
raise RuntimeError('Failed to fully update') raise RuntimeError('Failed to fully update')
finally: finally:
# pypy: set journal mode after possible upgrade db:
if pypy:
cur.execute("PRAGMA journal_mode = MEMORY")
cur.close() cur.close()
@property @property
@ -247,13 +262,14 @@ class Fail2BanDb(object):
A timestamped backup is also created prior to attempting the update. A timestamped backup is also created prior to attempting the update.
""" """
self._dbBackupFilename = self.filename + '.' + time.strftime('%Y%m%d-%H%M%S', MyTime.gmtime())
shutil.copyfile(self.filename, self._dbBackupFilename)
logSys.info("Database backup created: %s", self._dbBackupFilename)
if version > Fail2BanDb.__version__: if version > Fail2BanDb.__version__:
raise NotImplementedError( raise NotImplementedError(
"Attempt to travel to future version of database ...how did you get here??") "Attempt to travel to future version of database ...how did you get here??")
self._dbBackupFilename = self.filename + '.' + time.strftime('%Y%m%d-%H%M%S', MyTime.gmtime())
shutil.copyfile(self.filename, self._dbBackupFilename)
logSys.info("Database backup created: %s", self._dbBackupFilename)
if version < 2: if version < 2:
cur.executescript("BEGIN TRANSACTION;" cur.executescript("BEGIN TRANSACTION;"
"CREATE TEMPORARY TABLE logs_temp AS SELECT * FROM logs;" "CREATE TEMPORARY TABLE logs_temp AS SELECT * FROM logs;"