1
0
mirror of https://github.com/moparisthebest/SickRage synced 2024-11-11 03:45:01 -05:00

Merge branch 'origin/dev'

This commit is contained in:
echel0n 2014-06-05 13:20:36 -07:00
commit b0012708c9

View File

@ -20,16 +20,17 @@ from __future__ import with_statement
import os.path
import re
import threading
import sqlite3
import time
import sickbeard
import threading
from sqlalchemy import create_engine, exc
import sickbeard
from sickbeard import encodingKludge as ek
from sickbeard import logger
from sickbeard.exceptions import ex
from sickbeard.common import cpu_presets
from itertools import ifilter
db_lock = threading.Lock()
@ -51,9 +52,6 @@ class DBConnection:
def __init__(self, filename="sickbeard.db", suffix=None, row_type=None):
self.filename = filename
#engine = create_engine('sqlite:///' + dbFilename(filename))
#self.connection = engine.connect()
self.connection = sqlite3.connect(dbFilename(filename), 20)
if row_type == "dict":
self.connection.row_factory = self._dict_factory
@ -65,7 +63,7 @@ class DBConnection:
try:
result = self.select("SELECT db_version FROM db_version")
except exc.OperationalError, e:
except sqlite3.OperationalError, e:
if "no such table: db_version" in e.args[0]:
return 0
@ -103,7 +101,7 @@ class DBConnection:
if "unable to open database file" in e.args[0] or "database is locked" in e.args[0]:
logger.log(u"DB error: " + ex(e), logger.WARNING)
attempt += 1
time.sleep(1)
time.sleep(0.02)
else:
logger.log(u"DB error: " + ex(e), logger.ERROR)
raise
@ -125,8 +123,13 @@ class DBConnection:
sqlResult = []
attempt = 0
# Transaction
self.connection.isolation_level = None
self.connection.execute('BEGIN')
while attempt < 5:
try:
for qu in querylist:
if len(qu) == 1:
if logTransaction:
@ -138,6 +141,7 @@ class DBConnection:
sqlResult.append(self.connection.execute(qu[0], qu[1]))
self.connection.commit()
logger.log(u"Transaction with " + str(len(querylist)) + u" queries executed", logger.DEBUG)
return sqlResult
except sqlite3.OperationalError, e:
@ -147,7 +151,7 @@ class DBConnection:
if "unable to open database file" in e.args[0] or "database is locked" in e.args[0]:
logger.log(u"DB error: " + ex(e), logger.WARNING)
attempt += 1
time.sleep(1)
time.sleep(0.02)
else:
logger.log(u"DB error: " + ex(e), logger.ERROR)
raise
@ -178,13 +182,14 @@ class DBConnection:
else:
logger.log(self.filename + ": " + query + " with args " + str(args), logger.DB)
sqlResult = self.connection.execute(query, args)
self.connection.commit()
# get out of the connection attempt loop since we were successful
break
except sqlite3.OperationalError, e:
if "unable to open database file" in e.args[0] or "database is locked" in e.args[0]:
logger.log(u"DB error: " + ex(e), logger.WARNING)
attempt += 1
time.sleep(1)
time.sleep(0.02)
else:
logger.log(u"DB error: " + ex(e), logger.ERROR)
raise
@ -213,7 +218,7 @@ class DBConnection:
query = "UPDATE " + tableName + " SET " + ", ".join(genParams(valueDict)) + " WHERE " + " AND ".join(
genParams(keyDict))
result = self.action(query, valueDict.values() + keyDict.values())
self.action(query, valueDict.values() + keyDict.values())
if self.connection.total_changes == changesBefore:
query = "INSERT INTO " + tableName + " (" + ", ".join(valueDict.keys() + keyDict.keys()) + ")" + \
@ -238,6 +243,7 @@ class DBConnection:
def hasTable(self, tableName):
return len(self.action("SELECT 1 FROM sqlite_master WHERE name = ?;", (tableName, )).fetchall()) > 0
def sanityCheckDatabase(connection, sanity_check):
sanity_check(connection).check()
@ -249,6 +255,7 @@ class DBSanityCheck(object):
def check(self):
pass
# ===============
# = Upgrade API =
# ===============
@ -269,7 +276,7 @@ def _processUpgrade(connection, upgradeClass):
logger.log(u"Database upgrade required: " + prettyName(upgradeClass.__name__), logger.MESSAGE)
try:
instance.execute()
except exc.DatabaseError, e:
except sqlite3.DatabaseError, e:
print "Error in " + str(upgradeClass.__name__) + ": " + ex(e)
raise
logger.log(upgradeClass.__name__ + " upgrade completed", logger.DEBUG)