Commit 188fb85b authored by Philipp Hörist's avatar Philipp Hörist

Move DB creating into logger module

parent efd2ca50
......@@ -25,124 +25,12 @@
import os
import sys
import sqlite3
from gajim.common import app
from gajim.common import logger
from gajim.common import configpaths
from gajim.common.const import PathType
def create_log_db():
print(_('creating logs database'))
con = sqlite3.connect(logger.LOG_DB_PATH)
os.chmod(logger.LOG_DB_PATH, 0o600) # rw only for us
cur = con.cursor()
# create the tables
# kind can be
# status, gcstatus, gc_msg, (we only recv for those 3),
# single_msg_recv, chat_msg_recv, chat_msg_sent, single_msg_sent
# to meet all our needs
# logs.jid_id --> jids.jid_id but Sqlite doesn't do FK etc so it's done in python code
# jids.jid text column will be JID if TC-related, room_jid if GC-related,
# ROOM_JID/nick if pm-related.
# also check optparser.py, which updates databases on gajim updates
cur.executescript(
'''
CREATE TABLE jids(
jid_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
jid TEXT UNIQUE,
type INTEGER
);
CREATE TABLE unread_messages(
message_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
jid_id INTEGER,
shown BOOLEAN default 0
);
CREATE INDEX idx_unread_messages_jid_id ON unread_messages (jid_id);
CREATE TABLE logs(
log_line_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
account_id INTEGER,
jid_id INTEGER,
contact_name TEXT,
time INTEGER,
kind INTEGER,
show INTEGER,
message TEXT,
subject TEXT,
additional_data TEXT,
stanza_id TEXT,
encryption TEXT,
encryption_state TEXT,
marker INTEGER
);
CREATE TABLE last_archive_message(
jid_id INTEGER PRIMARY KEY UNIQUE,
last_mam_id TEXT,
oldest_mam_timestamp TEXT,
last_muc_timestamp TEXT
);
CREATE INDEX idx_logs_jid_id_time ON logs (jid_id, time DESC);
'''
)
con.commit()
con.close()
def create_cache_db():
print(_('creating cache database'))
con = sqlite3.connect(logger.CACHE_DB_PATH)
os.chmod(logger.CACHE_DB_PATH, 0o600) # rw only for us
cur = con.cursor()
cur.executescript(
'''
CREATE TABLE transports_cache (
transport TEXT UNIQUE,
type INTEGER
);
CREATE TABLE caps_cache (
hash_method TEXT,
hash TEXT,
data BLOB,
last_seen INTEGER);
CREATE TABLE rooms_last_message_time(
jid_id INTEGER PRIMARY KEY UNIQUE,
time INTEGER
);
CREATE TABLE IF NOT EXISTS roster_entry(
account_jid_id INTEGER,
jid_id INTEGER,
name TEXT,
subscription INTEGER,
ask BOOLEAN,
avatar_sha TEXT,
PRIMARY KEY (account_jid_id, jid_id)
);
CREATE TABLE IF NOT EXISTS roster_group(
account_jid_id INTEGER,
jid_id INTEGER,
group_name TEXT,
PRIMARY KEY (account_jid_id, jid_id, group_name)
);
'''
)
con.commit()
con.close()
def check_and_possibly_create_paths():
LOG_DB_PATH = configpaths.get('LOG_DB')
CACHE_DB_PATH = configpaths.get('CACHE_DB')
for path in configpaths.get_paths(PathType.FOLDER):
if not os.path.exists(path):
create_path(path)
......@@ -151,23 +39,6 @@ def check_and_possibly_create_paths():
print(_('Gajim will now exit'))
sys.exit()
if not os.path.exists(LOG_DB_PATH):
if os.path.exists(CACHE_DB_PATH):
os.remove(CACHE_DB_PATH)
create_log_db()
app.logger.init_vars()
elif os.path.isdir(LOG_DB_PATH):
print(_('%s is a directory but should be a file') % LOG_DB_PATH)
print(_('Gajim will now exit'))
sys.exit()
if not os.path.exists(CACHE_DB_PATH):
create_cache_db()
app.logger.attach_cache_database()
elif os.path.isdir(CACHE_DB_PATH):
print(_('%s is a directory but should be a file') % CACHE_DB_PATH)
print(_('Gajim will now exit'))
sys.exit()
def create_path(directory):
head, tail = os.path.split(directory)
......
......@@ -112,17 +112,132 @@ class Logger:
self.con = None
self.commit_timout_id = None
if os.path.isdir(LOG_DB_PATH):
print(_('%s is a directory but should be a file') % LOG_DB_PATH)
print(_('Gajim will now exit'))
sys.exit()
if os.path.isdir(CACHE_DB_PATH):
print(_('%s or %s is a directory but should be a file') % CACHE_DB_PATH)
print(_('Gajim will now exit'))
sys.exit()
if not os.path.exists(LOG_DB_PATH):
# this can happen only the first time (the time we create the db)
# db is not created here but in src/common/checks_paths.py
return
if os.path.exists(CACHE_DB_PATH):
os.remove(CACHE_DB_PATH)
self.create_log_db()
self.init_vars()
if not os.path.exists(CACHE_DB_PATH):
# this can happen cache database is not present when gajim is launched
# db will be created in src/common/checks_paths.py
return
self.create_cache_db()
self.attach_cache_database()
def create_log_db(self):
print(_('creating logs database'))
con = sqlite.connect(LOG_DB_PATH)
os.chmod(LOG_DB_PATH, 0o600) # rw only for us
cur = con.cursor()
# create the tables
# kind can be
# status, gcstatus, gc_msg, (we only recv for those 3),
# single_msg_recv, chat_msg_recv, chat_msg_sent, single_msg_sent
# to meet all our needs
# logs.jid_id --> jids.jid_id but Sqlite doesn't do FK etc so it's done in python code
# jids.jid text column will be JID if TC-related, room_jid if GC-related,
# ROOM_JID/nick if pm-related.
# also check optparser.py, which updates databases on gajim updates
cur.executescript(
'''
CREATE TABLE jids(
jid_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
jid TEXT UNIQUE,
type INTEGER
);
CREATE TABLE unread_messages(
message_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
jid_id INTEGER,
shown BOOLEAN default 0
);
CREATE INDEX idx_unread_messages_jid_id ON unread_messages (jid_id);
CREATE TABLE logs(
log_line_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
account_id INTEGER,
jid_id INTEGER,
contact_name TEXT,
time INTEGER,
kind INTEGER,
show INTEGER,
message TEXT,
subject TEXT,
additional_data TEXT,
stanza_id TEXT,
encryption TEXT,
encryption_state TEXT,
marker INTEGER
);
CREATE TABLE last_archive_message(
jid_id INTEGER PRIMARY KEY UNIQUE,
last_mam_id TEXT,
oldest_mam_timestamp TEXT,
last_muc_timestamp TEXT
);
CREATE INDEX idx_logs_jid_id_time ON logs (jid_id, time DESC);
'''
)
con.commit()
con.close()
def create_cache_db(self):
print(_('creating cache database'))
con = sqlite.connect(CACHE_DB_PATH)
os.chmod(CACHE_DB_PATH, 0o600) # rw only for us
cur = con.cursor()
cur.executescript(
'''
CREATE TABLE transports_cache (
transport TEXT UNIQUE,
type INTEGER
);
CREATE TABLE caps_cache (
hash_method TEXT,
hash TEXT,
data BLOB,
last_seen INTEGER);
CREATE TABLE rooms_last_message_time(
jid_id INTEGER PRIMARY KEY UNIQUE,
time INTEGER
);
CREATE TABLE IF NOT EXISTS roster_entry(
account_jid_id INTEGER,
jid_id INTEGER,
name TEXT,
subscription INTEGER,
ask BOOLEAN,
avatar_sha TEXT,
PRIMARY KEY (account_jid_id, jid_id)
);
CREATE TABLE IF NOT EXISTS roster_group(
account_jid_id INTEGER,
jid_id INTEGER,
group_name TEXT,
PRIMARY KEY (account_jid_id, jid_id, group_name)
);
'''
)
con.commit()
con.close()
@staticmethod
def namedtuple_factory(cursor, row):
"""
......
......@@ -161,9 +161,9 @@ class GajimApplication(Gtk.Application):
from gajim.common import logger
from gajim.common import caps_cache
try:
check_paths.check_and_possibly_create_paths()
app.logger = logger.Logger()
caps_cache.initialize(app.logger)
check_paths.check_and_possibly_create_paths()
except exceptions.DatabaseMalformed as error:
dlg = Gtk.MessageDialog(
None,
......
......@@ -1084,8 +1084,10 @@ if __name__ == '__main__':
from gajim.conversation_textview import ConversationTextview
from gajim.gui_interface import Interface
from gajim.common import app, logger, caps_cache
app.logger = logger.Logger()
caps_cache.initialize(app.logger)
# TODO: dont call Logger() it will create the DB
# maybe mock this object for tests
# app.logger = logger.Logger()
# caps_cache.initialize(app.logger)
Interface()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment