core: fixed bad database creation on first run in sqlite storage

This commit is contained in:
Goffi 2013-06-20 17:17:35 +02:00
commit 4d08b7f72e

View file

@ -32,13 +32,15 @@ class SqliteStorage(object):
"""Connect to the given database """Connect to the given database
@param db_filename: full path to the Sqlite database""" @param db_filename: full path to the Sqlite database"""
self.initialized = defer.Deferred() # triggered when memory is fully initialised and ready self.initialized = defer.Deferred() # triggered when memory is fully initialised and ready
init_defers = [] # list of deferred we have to wait to before initialisation is complete
self.profiles = {} # we keep cache for the profiles (key: profile name, value: profile id) self.profiles = {} # we keep cache for the profiles (key: profile name, value: profile id)
info(_("Connecting database")) info(_("Connecting database"))
new_base = not os.path.exists(db_filename) # do we have to create the database ? new_base = not os.path.exists(db_filename) # do we have to create the database ?
self.dbpool = adbapi.ConnectionPool("sqlite3", db_filename, check_same_thread=False) self.dbpool = adbapi.ConnectionPool("sqlite3", db_filename, check_same_thread=False)
init_defers.append(self.dbpool.runOperation("PRAGMA foreign_keys = ON").addErrback(lambda x: error(_("Can't activate foreign keys"))))
# init_defer is the initialisation deferred, initialisation is ok when all its callbacks have been done
init_defer = self.dbpool.runOperation("PRAGMA foreign_keys = ON").addErrback(lambda x: error(_("Can't activate foreign keys")))
if new_base: if new_base:
info(_("The database is new, creating the tables")) info(_("The database is new, creating the tables"))
database_creation = [ database_creation = [
@ -58,15 +60,14 @@ class SqliteStorage(object):
"CREATE TABLE private_ind_bin (namespace TEXT, key TEXT, profile_id INTEGER, value BLOB, PRIMARY KEY (namespace, key, profile_id), FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE)", "CREATE TABLE private_ind_bin (namespace TEXT, key TEXT, profile_id INTEGER, value BLOB, PRIMARY KEY (namespace, key, profile_id), FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE)",
] ]
for op in database_creation: for op in database_creation:
d = self.dbpool.runOperation(op) init_defer.addCallback(lambda ignore, sql: self.dbpool.runOperation(sql), op)
d.addErrback(lambda x: error(_("Error while creating tables in database [QUERY: %s]") % op)) init_defer.addErrback(lambda ignore, sql: error(_("Error while creating tables in database [QUERY: %s]") % sql, op))
init_defers.append(d)
def fillProfileCache(ignore): def fillProfileCache(ignore):
d = self.dbpool.runQuery("SELECT name,id FROM profiles").addCallback(self._profilesCache) d = self.dbpool.runQuery("SELECT name,id FROM profiles").addCallback(self._profilesCache)
d.chainDeferred(self.initialized) d.chainDeferred(self.initialized)
defer.DeferredList(init_defers).addCallback(fillProfileCache) init_defer.addCallback(fillProfileCache)
#Profiles #Profiles
def _profilesCache(self, profiles_result): def _profilesCache(self, profiles_result):