comparison 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
comparison
equal deleted inserted replaced
623:47d3a22b4629 624:70988f08d0ad
30 30
31 def __init__(self, db_filename): 31 def __init__(self, db_filename):
32 """Connect to the given database 32 """Connect to the given database
33 @param db_filename: full path to the Sqlite database""" 33 @param db_filename: full path to the Sqlite database"""
34 self.initialized = defer.Deferred() # triggered when memory is fully initialised and ready 34 self.initialized = defer.Deferred() # triggered when memory is fully initialised and ready
35 init_defers = [] # list of deferred we have to wait to before initialisation is complete
36 self.profiles = {} # we keep cache for the profiles (key: profile name, value: profile id) 35 self.profiles = {} # we keep cache for the profiles (key: profile name, value: profile id)
37 36
38 info(_("Connecting database")) 37 info(_("Connecting database"))
39 new_base = not os.path.exists(db_filename) # do we have to create the database ? 38 new_base = not os.path.exists(db_filename) # do we have to create the database ?
40 self.dbpool = adbapi.ConnectionPool("sqlite3", db_filename, check_same_thread=False) 39 self.dbpool = adbapi.ConnectionPool("sqlite3", db_filename, check_same_thread=False)
41 init_defers.append(self.dbpool.runOperation("PRAGMA foreign_keys = ON").addErrback(lambda x: error(_("Can't activate foreign keys")))) 40
41 # init_defer is the initialisation deferred, initialisation is ok when all its callbacks have been done
42 init_defer = self.dbpool.runOperation("PRAGMA foreign_keys = ON").addErrback(lambda x: error(_("Can't activate foreign keys")))
43
42 if new_base: 44 if new_base:
43 info(_("The database is new, creating the tables")) 45 info(_("The database is new, creating the tables"))
44 database_creation = [ 46 database_creation = [
45 "CREATE TABLE profiles (id INTEGER PRIMARY KEY ASC, name TEXT, UNIQUE (name))", 47 "CREATE TABLE profiles (id INTEGER PRIMARY KEY ASC, name TEXT, UNIQUE (name))",
46 "CREATE TABLE message_types (type TEXT PRIMARY KEY)", 48 "CREATE TABLE message_types (type TEXT PRIMARY KEY)",
56 "CREATE TABLE private_ind (namespace TEXT, key TEXT, profile_id INTEGER, value TEXT, PRIMARY KEY (namespace, key, profile_id), FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE)", 58 "CREATE TABLE private_ind (namespace TEXT, key TEXT, profile_id INTEGER, value TEXT, PRIMARY KEY (namespace, key, profile_id), FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE)",
57 "CREATE TABLE private_gen_bin (namespace TEXT, key TEXT, value BLOB, PRIMARY KEY (namespace, key))", 59 "CREATE TABLE private_gen_bin (namespace TEXT, key TEXT, value BLOB, PRIMARY KEY (namespace, key))",
58 "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)", 60 "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)",
59 ] 61 ]
60 for op in database_creation: 62 for op in database_creation:
61 d = self.dbpool.runOperation(op) 63 init_defer.addCallback(lambda ignore, sql: self.dbpool.runOperation(sql), op)
62 d.addErrback(lambda x: error(_("Error while creating tables in database [QUERY: %s]") % op)) 64 init_defer.addErrback(lambda ignore, sql: error(_("Error while creating tables in database [QUERY: %s]") % sql, op))
63 init_defers.append(d)
64 65
65 def fillProfileCache(ignore): 66 def fillProfileCache(ignore):
66 d = self.dbpool.runQuery("SELECT name,id FROM profiles").addCallback(self._profilesCache) 67 d = self.dbpool.runQuery("SELECT name,id FROM profiles").addCallback(self._profilesCache)
67 d.chainDeferred(self.initialized) 68 d.chainDeferred(self.initialized)
68 69
69 defer.DeferredList(init_defers).addCallback(fillProfileCache) 70 init_defer.addCallback(fillProfileCache)
70 71
71 #Profiles 72 #Profiles
72 def _profilesCache(self, profiles_result): 73 def _profilesCache(self, profiles_result):
73 """Fill the profiles cache 74 """Fill the profiles cache
74 @param profiles_result: result of the sql profiles query""" 75 @param profiles_result: result of the sql profiles query"""