Merge branch 'refactor_db_update' into 'master'

refactor the way we call SQL

See merge request gajim/gajim!162
This commit is contained in:
Yann Leboulanger 2017-11-30 22:47:42 +01:00
commit 91ed375b97
1 changed files with 120 additions and 262 deletions

View File

@ -267,7 +267,7 @@ class OptionsParser:
try: try:
cur.executescript( cur.executescript(
''' '''
CREATE TABLE unread_messages ( CREATE TABLE IF NOT EXISTS unread_messages (
message_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, message_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
jid_id INTEGER jid_id INTEGER
); );
@ -299,6 +299,20 @@ class OptionsParser:
app.config.set_per('accounts', account, 'file_transfer_proxies', app.config.set_per('accounts', account, 'file_transfer_proxies',
proxies_str) proxies_str)
@staticmethod
def call_sql(db_path, sql):
con = sqlite.connect(db_path)
cur = con.cursor()
try:
cur.executescript(sql)
con.commit()
except sqlite.OperationalError as e:
if str(e).startswith('duplicate column name:'):
log.info(str(e))
else:
log.exception('Error')
con.close()
def update_config_x_to_09(self): def update_config_x_to_09(self):
# Var name that changed: # Var name that changed:
# avatar_width /height -> chat_avatar_width / height # avatar_width /height -> chat_avatar_width / height
@ -384,25 +398,14 @@ class OptionsParser:
""" """
Create table transports_cache if there is no such table Create table transports_cache if there is no such table
""" """
# FIXME see #2812 self.call_sql(logger.LOG_DB_PATH,
back = os.getcwd() '''
os.chdir(logger.LOG_DB_FOLDER) CREATE TABLE IF NOT EXISTS transports_cache (
con = sqlite.connect(logger.LOG_DB_FILE) transport TEXT UNIQUE,
os.chdir(back) type INTEGER
cur = con.cursor() );
try: '''
cur.executescript( )
'''
CREATE TABLE transports_cache (
transport TEXT UNIQUE,
type INTEGER
);
'''
)
con.commit()
except sqlite.OperationalError:
pass
con.close()
app.config.set('version', '0.10.1.3') app.config.set('version', '0.10.1.3')
def update_config_to_01014(self): def update_config_to_01014(self):
@ -410,36 +413,19 @@ class OptionsParser:
Apply indeces to the logs database Apply indeces to the logs database
""" """
print(_('migrating logs database to indices')) print(_('migrating logs database to indices'))
# FIXME see #2812 self.call_sql(logger.LOG_DB_PATH,
back = os.getcwd() '''
os.chdir(logger.LOG_DB_FOLDER) CREATE INDEX idx_logs_jid_id_kind ON logs (jid_id, kind);
con = sqlite.connect(logger.LOG_DB_FILE) CREATE INDEX idx_unread_messages_jid_id ON unread_messages (jid_id);
os.chdir(back) '''
cur = con.cursor() )
# apply indeces
try:
cur.executescript(
'''
CREATE INDEX idx_logs_jid_id_kind ON logs (jid_id, kind);
CREATE INDEX idx_unread_messages_jid_id ON unread_messages (jid_id);
'''
)
con.commit()
except Exception:
pass
con.close()
app.config.set('version', '0.10.1.4') app.config.set('version', '0.10.1.4')
def update_config_to_01015(self): def update_config_to_01015(self):
""" """
Clean show values in logs database Clean show values in logs database
""" """
#FIXME see #2812 con = sqlite.connect(logger.LOG_DB_PATH)
back = os.getcwd()
os.chdir(logger.LOG_DB_FOLDER)
con = sqlite.connect(logger.LOG_DB_FILE)
os.chdir(back)
cur = con.cursor() cur = con.cursor()
status = dict((i[5:].lower(), logger.constants.__dict__[i]) for i in \ status = dict((i[5:].lower(), logger.constants.__dict__[i]) for i in \
logger.constants.__dict__.keys() if i.startswith('SHOW_')) logger.constants.__dict__.keys() if i.startswith('SHOW_'))
@ -520,27 +506,16 @@ class OptionsParser:
app.config.set('version', '0.11.1.2') app.config.set('version', '0.11.1.2')
def update_config_to_01113(self): def update_config_to_01113(self):
# copy&pasted from update_config_to_01013, possibly 'FIXME see #2812' applies too self.call_sql(logger.LOG_DB_PATH,
back = os.getcwd() '''
os.chdir(logger.LOG_DB_FOLDER) CREATE TABLE IF NOT EXISTS caps_cache (
con = sqlite.connect(logger.LOG_DB_FILE) node TEXT,
os.chdir(back) ver TEXT,
cur = con.cursor() ext TEXT,
try: data BLOB
cur.executescript( );
''' '''
CREATE TABLE caps_cache ( )
node TEXT,
ver TEXT,
ext TEXT,
data BLOB
);
'''
)
con.commit()
except sqlite.OperationalError:
pass
con.close()
app.config.set('version', '0.11.1.3') app.config.set('version', '0.11.1.3')
def update_config_to_01114(self): def update_config_to_01114(self):
@ -567,22 +542,7 @@ class OptionsParser:
app.config.set('version', '0.11.1.4') app.config.set('version', '0.11.1.4')
def update_config_to_01115(self): def update_config_to_01115(self):
# copy&pasted from update_config_to_01013, possibly 'FIXME see #2812' applies too self.call_sql(logger.LOG_DB_PATH, '''DELETE FROM caps_cache;''')
back = os.getcwd()
os.chdir(logger.LOG_DB_FOLDER)
con = sqlite.connect(logger.LOG_DB_FILE)
os.chdir(back)
cur = con.cursor()
try:
cur.executescript(
'''
DELETE FROM caps_cache;
'''
)
con.commit()
except sqlite.OperationalError:
pass
con.close()
app.config.set('version', '0.11.1.5') app.config.set('version', '0.11.1.5')
def update_config_to_01121(self): def update_config_to_01121(self):
@ -599,26 +559,16 @@ class OptionsParser:
app.config.set('version', '0.11.2.1') app.config.set('version', '0.11.2.1')
def update_config_to_01141(self): def update_config_to_01141(self):
back = os.getcwd() self.call_sql(logger.LOG_DB_PATH,
os.chdir(logger.LOG_DB_FOLDER) '''
con = sqlite.connect(logger.LOG_DB_FILE) CREATE TABLE IF NOT EXISTS caps_cache (
os.chdir(back) node TEXT,
cur = con.cursor() ver TEXT,
try: ext TEXT,
cur.executescript( data BLOB
''' );
CREATE TABLE IF NOT EXISTS caps_cache ( '''
node TEXT, )
ver TEXT,
ext TEXT,
data BLOB
);
'''
)
con.commit()
except sqlite.OperationalError:
pass
con.close()
app.config.set('version', '0.11.4.1') app.config.set('version', '0.11.4.1')
def update_config_to_01142(self): def update_config_to_01142(self):
@ -640,24 +590,14 @@ class OptionsParser:
app.config.set('version', '0.11.1.2') app.config.set('version', '0.11.1.2')
def update_config_to_01143(self): def update_config_to_01143(self):
back = os.getcwd() self.call_sql(logger.LOG_DB_PATH,
os.chdir(logger.LOG_DB_FOLDER) '''
con = sqlite.connect(logger.LOG_DB_FILE) CREATE TABLE IF NOT EXISTS rooms_last_message_time(
os.chdir(back) jid_id INTEGER PRIMARY KEY UNIQUE,
cur = con.cursor() time INTEGER
try: );
cur.executescript( '''
''' )
CREATE TABLE IF NOT EXISTS rooms_last_message_time(
jid_id INTEGER PRIMARY KEY UNIQUE,
time INTEGER
);
'''
)
con.commit()
except sqlite.OperationalError:
pass
con.close()
app.config.set('version', '0.11.4.3') app.config.set('version', '0.11.4.3')
def update_config_to_01144(self): def update_config_to_01144(self):
@ -674,7 +614,7 @@ class OptionsParser:
try: try:
cur.executescript( cur.executescript(
''' '''
CREATE TABLE caps_cache ( CREATE TABLE IF NOT EXISTS caps_cache (
hash_method TEXT, hash_method TEXT,
hash TEXT, hash TEXT,
data BLOB data BLOB
@ -752,35 +692,25 @@ class OptionsParser:
app.config.set('version', '0.12.1.5') app.config.set('version', '0.12.1.5')
def update_config_to_01231(self): def update_config_to_01231(self):
back = os.getcwd() self.call_sql(logger.LOG_DB_PATH,
os.chdir(logger.LOG_DB_FOLDER) '''
con = sqlite.connect(logger.LOG_DB_FILE) CREATE TABLE IF NOT EXISTS roster_entry(
os.chdir(back) account_jid_id INTEGER,
cur = con.cursor() jid_id INTEGER,
try: name TEXT,
cur.executescript( subscription INTEGER,
''' ask BOOLEAN,
CREATE TABLE IF NOT EXISTS roster_entry( PRIMARY KEY (account_jid_id, jid_id)
account_jid_id INTEGER, );
jid_id INTEGER,
name TEXT,
subscription INTEGER,
ask BOOLEAN,
PRIMARY KEY (account_jid_id, jid_id)
);
CREATE TABLE IF NOT EXISTS roster_group( CREATE TABLE IF NOT EXISTS roster_group(
account_jid_id INTEGER, account_jid_id INTEGER,
jid_id INTEGER, jid_id INTEGER,
group_name TEXT, group_name TEXT,
PRIMARY KEY (account_jid_id, jid_id, group_name) PRIMARY KEY (account_jid_id, jid_id, group_name)
); );
''' '''
) )
con.commit()
except sqlite.OperationalError:
pass
con.close()
app.config.set('version', '0.12.3.1') app.config.set('version', '0.12.3.1')
def update_config_from_0125(self): def update_config_from_0125(self):
@ -792,22 +722,10 @@ class OptionsParser:
self.update_config_to_01231() self.update_config_to_01231()
def update_config_to_01251(self): def update_config_to_01251(self):
back = os.getcwd() self.call_sql(logger.LOG_DB_PATH,
os.chdir(logger.LOG_DB_FOLDER) '''ALTER TABLE unread_messages
con = sqlite.connect(logger.LOG_DB_FILE) ADD COLUMN 'shown' BOOLEAN default 0;'''
os.chdir(back) )
cur = con.cursor()
try:
cur.executescript(
'''
ALTER TABLE unread_messages
ADD shown BOOLEAN default 0;
'''
)
con.commit()
except sqlite.OperationalError:
pass
con.close()
app.config.set('version', '0.12.5.1') app.config.set('version', '0.12.5.1')
def update_config_to_01252(self): def update_config_to_01252(self):
@ -881,43 +799,21 @@ class OptionsParser:
app.config.set('version', '0.12.5.8') app.config.set('version', '0.12.5.8')
def update_config_to_013100(self): def update_config_to_013100(self):
back = os.getcwd() self.call_sql(logger.LOG_DB_PATH,
os.chdir(logger.LOG_DB_FOLDER) '''ALTER TABLE caps_cache
con = sqlite.connect(logger.LOG_DB_FILE) ADD COLUMN 'last_seen' INTEGER default %d;''' % int(time())
os.chdir(back) )
cur = con.cursor()
try:
cur.executescript(
'''
ALTER TABLE caps_cache
ADD last_seen INTEGER default %d;
''' % int(time())
)
con.commit()
except sqlite.OperationalError:
pass
con.close()
app.config.set('version', '0.13.10.0') app.config.set('version', '0.13.10.0')
def update_config_to_013101(self): def update_config_to_013101(self):
back = os.getcwd() self.call_sql(logger.LOG_DB_PATH,
os.chdir(logger.LOG_DB_FOLDER) '''
con = sqlite.connect(logger.LOG_DB_FILE) DROP INDEX IF EXISTS idx_logs_jid_id_kind;
os.chdir(back)
cur = con.cursor()
try:
cur.executescript(
'''
DROP INDEX IF EXISTS idx_logs_jid_id_kind;
CREATE INDEX IF NOT EXISTS CREATE INDEX IF NOT EXISTS
idx_logs_jid_id_time ON logs (jid_id, time DESC); idx_logs_jid_id_time ON logs (jid_id, time DESC);
''' '''
) )
con.commit()
except sqlite.OperationalError:
pass
con.close()
app.config.set('version', '0.13.10.1') app.config.set('version', '0.13.10.1')
def update_config_to_013901(self): def update_config_to_013901(self):
@ -971,44 +867,21 @@ class OptionsParser:
for account in self.old_values['accounts'].keys(): for account in self.old_values['accounts'].keys():
app.config.del_per('accounts', account, 'minimized_gc') app.config.del_per('accounts', account, 'minimized_gc')
back = os.getcwd() self.call_sql(logger.LOG_DB_PATH,
os.chdir(logger.LOG_DB_FOLDER) '''ALTER TABLE logs
con = sqlite.connect(logger.LOG_DB_FILE) ADD COLUMN 'additinal_data' TEXT default '()';'''
os.chdir(back) )
cur = con.cursor()
try:
cur.executescript(
'''
ALTER TABLE logs ADD COLUMN 'additional_data' TEXT DEFAULT '{}';
'''
)
con.commit()
except sqlite.OperationalError:
pass
con.close()
app.config.set('version', '0.16.10.2') app.config.set('version', '0.16.10.2')
def update_config_to_016103(self): def update_config_to_016103(self):
back = os.getcwd() self.call_sql(logger.LOG_DB_PATH,
os.chdir(logger.LOG_DB_FOLDER) '''ALTER TABLE logs ADD COLUMN 'stanza_id' TEXT;
con = sqlite.connect(logger.LOG_DB_FILE) ALTER TABLE logs ADD COLUMN 'mam_id' TEXT;
os.chdir(back) ALTER TABLE logs ADD COLUMN 'encryption' TEXT;
cur = con.cursor() ALTER TABLE logs ADD COLUMN 'encryption_state' TEXT;
try: ALTER TABLE logs ADD COLUMN 'marker' INTEGER;
cur.executescript( '''
''' )
ALTER TABLE logs ADD COLUMN 'stanza_id' TEXT;
ALTER TABLE logs ADD COLUMN 'mam_id' TEXT;
ALTER TABLE logs ADD COLUMN 'encryption' TEXT;
ALTER TABLE logs ADD COLUMN 'encryption_state' TEXT;
ALTER TABLE logs ADD COLUMN 'marker' INTEGER;
'''
)
con.commit()
except sqlite.OperationalError:
pass
con.close()
app.config.set('version', '0.16.10.3') app.config.set('version', '0.16.10.3')
def update_config_to_016104(self): def update_config_to_016104(self):
@ -1021,37 +894,22 @@ class OptionsParser:
app.config.set('version', '0.16.10.5') app.config.set('version', '0.16.10.5')
def update_config_to_016111(self): def update_config_to_016111(self):
con = sqlite.connect(logger.CACHE_DB_PATH) self.call_sql(logger.CACHE_DB_PATH,
cur = con.cursor() '''ALTER TABLE roster_entry ADD COLUMN 'avatar_sha' TEXT;
try: '''
cur.executescript( )
'''
ALTER TABLE roster_entry ADD COLUMN 'avatar_sha' TEXT;
'''
)
con.commit()
except sqlite.OperationalError:
log.exception('Error')
con.close()
app.config.set('version', '0.16.11.1') app.config.set('version', '0.16.11.1')
def update_config_to_016112(self): def update_config_to_016112(self):
con = sqlite.connect(logger.LOG_DB_PATH) self.call_sql(logger.LOG_DB_PATH,
cur = con.cursor() '''
try: CREATE TABLE IF NOT EXISTS last_archive_message(
cur.executescript( jid_id INTEGER PRIMARY KEY UNIQUE,
''' last_mam_id TEXT,
CREATE TABLE IF NOT EXISTS last_archive_message( oldest_mam_timestamp TEXT,
jid_id INTEGER PRIMARY KEY UNIQUE, last_muc_timestamp TEXT
last_mam_id TEXT, );
oldest_mam_timestamp TEXT, ALTER TABLE logs ADD COLUMN 'account_jid' INTEGER;
last_muc_timestamp TEXT '''
); )
ALTER TABLE logs ADD COLUMN 'account_id' INTEGER;
'''
)
con.commit()
except sqlite.OperationalError:
log.exception('Error')
con.close()
app.config.set('version', '0.16.11.2') app.config.set('version', '0.16.11.2')