Commit 041a3eda authored by Julien Muchembled's avatar Julien Muchembled

mysql: code clean up

parent 9b33b1db
...@@ -20,6 +20,9 @@ from MySQLdb import DataError, IntegrityError, \ ...@@ -20,6 +20,9 @@ from MySQLdb import DataError, IntegrityError, \
OperationalError, ProgrammingError OperationalError, ProgrammingError
from MySQLdb.constants.CR import SERVER_GONE_ERROR, SERVER_LOST from MySQLdb.constants.CR import SERVER_GONE_ERROR, SERVER_LOST
from MySQLdb.constants.ER import DATA_TOO_LONG, DUP_ENTRY, NO_SUCH_TABLE from MySQLdb.constants.ER import DATA_TOO_LONG, DUP_ENTRY, NO_SUCH_TABLE
# BBB: the following 2 constants were added to mysqlclient 1.3.8
DROP_LAST_PARTITION = 1508
SAME_NAME_PARTITION = 1517
from array import array from array import array
from hashlib import sha1 from hashlib import sha1
import os import os
...@@ -121,10 +124,11 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -121,10 +124,11 @@ class MySQLDatabaseManager(DatabaseManager):
for d in row]) for d in row])
for row in r.fetch_row(r.num_rows())]) for row in r.fetch_row(r.num_rows())])
break break
except OperationalError, m: except OperationalError as m:
if self._active or m[0] not in (SERVER_GONE_ERROR, SERVER_LOST): code, m = m.args
if self._active or SERVER_GONE_ERROR != code != SERVER_LOST:
raise DatabaseFailure('MySQL error %d: %s\nQuery: %s' raise DatabaseFailure('MySQL error %d: %s\nQuery: %s'
% (m[0], m[1], getPrintableQuery(query[:1000]))) % (code, m, getPrintableQuery(query[:1000])))
logging.info('the MySQL server is gone; reconnecting') logging.info('the MySQL server is gone; reconnecting')
self._connect() self._connect()
r = query.split(None, 1)[0] r = query.split(None, 1)[0]
...@@ -145,8 +149,8 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -145,8 +149,8 @@ class MySQLDatabaseManager(DatabaseManager):
def nonempty(self, table): def nonempty(self, table):
try: try:
return bool(self.query("SELECT 1 FROM %s LIMIT 1" % table)) return bool(self.query("SELECT 1 FROM %s LIMIT 1" % table))
except ProgrammingError, (code, _): except ProgrammingError as e:
if code != NO_SUCH_TABLE: if e.args[0] != NO_SUCH_TABLE:
raise raise
def _setup(self): def _setup(self):
...@@ -276,8 +280,8 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -276,8 +280,8 @@ class MySQLDatabaseManager(DatabaseManager):
sql = "REPLACE INTO config VALUES ('%s', '%s')" % (k, e(value)) sql = "REPLACE INTO config VALUES ('%s', '%s')" % (k, e(value))
try: try:
q(sql) q(sql)
except DataError, (code, _): except DataError as e:
if code != DATA_TOO_LONG or len(value) < 256 or key != "zodb": if e.args[0] != DATA_TOO_LONG or len(value) < 256 or key != "zodb":
raise raise
q("ALTER TABLE config MODIFY value VARBINARY(%s) NULL" % len(value)) q("ALTER TABLE config MODIFY value VARBINARY(%s) NULL" % len(value))
q(sql) q(sql)
...@@ -380,8 +384,8 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -380,8 +384,8 @@ class MySQLDatabaseManager(DatabaseManager):
for table in 'trans', 'obj': for table in 'trans', 'obj':
try: try:
self.conn.query(add % table) self.conn.query(add % table)
except OperationalError, (code, _): except OperationalError as e:
if code != 1517: # duplicate partition name if e.args[0] != SAME_NAME_PARTITION:
raise raise
def dropPartitions(self, offset_list): def dropPartitions(self, offset_list):
...@@ -404,8 +408,8 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -404,8 +408,8 @@ class MySQLDatabaseManager(DatabaseManager):
for table in 'trans', 'obj': for table in 'trans', 'obj':
try: try:
self.conn.query(drop % table) self.conn.query(drop % table)
except OperationalError, (code, _): except OperationalError as e:
if code != 1508: # already dropped if e.args[0] != DROP_LAST_PARTITION:
raise raise
def dropUnfinishedData(self): def dropUnfinishedData(self):
...@@ -531,8 +535,8 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -531,8 +535,8 @@ class MySQLDatabaseManager(DatabaseManager):
try: try:
self.query("INSERT INTO data VALUES (NULL, '%s', %d, '%s')" % self.query("INSERT INTO data VALUES (NULL, '%s', %d, '%s')" %
(checksum, compression, e(data))) (checksum, compression, e(data)))
except IntegrityError, (code, _): except IntegrityError as e:
if code == DUP_ENTRY: if e.args[0] == DUP_ENTRY:
(r, d), = self.query("SELECT id, value FROM data" (r, d), = self.query("SELECT id, value FROM data"
" WHERE hash='%s' AND compression=%s" " WHERE hash='%s' AND compression=%s"
% (checksum, compression)) % (checksum, compression))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment