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