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
@param db_filename: full path to the Sqlite database"""
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)
info(_("Connecting 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)
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:
info(_("The database is new, creating the tables"))
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)",
]
for op in database_creation:
d = self.dbpool.runOperation(op)
d.addErrback(lambda x: error(_("Error while creating tables in database [QUERY: %s]") % op))
init_defers.append(d)
init_defer.addCallback(lambda ignore, sql: self.dbpool.runOperation(sql), op)
init_defer.addErrback(lambda ignore, sql: error(_("Error while creating tables in database [QUERY: %s]") % sql, op))
def fillProfileCache(ignore):
d = self.dbpool.runQuery("SELECT name,id FROM profiles").addCallback(self._profilesCache)
d.chainDeferred(self.initialized)
defer.DeferredList(init_defers).addCallback(fillProfileCache)
init_defer.addCallback(fillProfileCache)
#Profiles
def _profilesCache(self, profiles_result):