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