diff src/memory/sqlite.py @ 624:70988f08d0ad

core: fixed bad database creation on first run in sqlite storage
author Goffi <goffi@goffi.org>
date Thu, 20 Jun 2013 17:17:35 +0200
parents 84a6e83157c2
children ffb716804580
line wrap: on
line diff
--- a/src/memory/sqlite.py	Thu Jun 20 17:15:21 2013 +0200
+++ b/src/memory/sqlite.py	Thu Jun 20 17:17:35 2013 +0200
@@ -32,13 +32,15 @@
         """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 @@
                 "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):