Dispatch: better (aka correct) db_init section

This commit is contained in:
Brian S. Stephan 2013-05-03 15:59:49 -05:00
parent b375a5a049
commit f09a41bce0
1 changed files with 19 additions and 9 deletions

View File

@ -31,10 +31,10 @@ class Dispatch(Module):
def db_init(self): def db_init(self):
"""Set up the database tables, if they don't exist.""" """Set up the database tables, if they don't exist."""
version = self.db_module_registered(self.__class__.__name__) try:
if (version == None):
db = self.get_db() db = self.get_db()
try: version = self.db_module_registered(self.__class__.__name__)
if version is None:
version = 1 version = 1
cur = db.cursor(mdb.cursors.DictCursor) cur = db.cursor(mdb.cursors.DictCursor)
cur.execute(''' cur.execute('''
@ -48,12 +48,22 @@ class Dispatch(Module):
db.commit() db.commit()
self.db_register_module_version(self.__class__.__name__, self.db_register_module_version(self.__class__.__name__,
version) version)
except mdb.Error as e: if version == 1:
db.rollback() version = 2
self.log.error("database error trying to create tables") cur = db.cursor(mdb.cursors.DictCursor)
self.log.exception(e) cur.execute('''
raise ALTER TABLE dispatch_item DROP PRIMARY KEY, ADD PRIMARY KEY (key_, dest)
finally: cur.close() ''')
db.commit()
self.db_register_module_version(self.__class__.__name__,
version)
except mdb.Error as e:
db.rollback()
self.log.error("database error trying to create tables")
self.log.exception(e)
raise
finally: cur.close()
def xmlrpc_init(self): def xmlrpc_init(self):
"""Expose dispatching interface.""" """Expose dispatching interface."""