Add table `config` for meta settings.

master
Ethan Dalool 2017-11-15 20:40:52 -08:00
parent 2b71ec3c76
commit 7ef5a8ceee
1 changed files with 21 additions and 0 deletions

View File

@ -30,6 +30,11 @@ DATABASE_VERSION = 1
DB_INIT = ''' DB_INIT = '''
PRAGMA user_version = {user_version}; PRAGMA user_version = {user_version};
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS config(
key TEXT,
value TEXT
);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS submissions( CREATE TABLE IF NOT EXISTS submissions(
idint INT, idint INT,
idstr TEXT, idstr TEXT,
@ -72,6 +77,9 @@ ERROR_DATABASE_OUTOFDATE = '''
Database is out of date. {current} should be {new}. Database is out of date. {current} should be {new}.
'''.strip() '''.strip()
DEFAULT_CONFIG = {
}
SQL_SUBMISSION_COLUMNS = [ SQL_SUBMISSION_COLUMNS = [
'idint', 'idint',
'idstr', 'idstr',
@ -144,6 +152,19 @@ class TSDB:
self.cur.execute(statement) self.cur.execute(statement)
self.sql.commit() self.sql.commit()
self.config = {}
for (key, default_value) in DEFAULT_CONFIG.items():
self.cur.execute('SELECT value FROM config WHERE key == ?', [key])
existing_value = self.cur.fetchone()
if existing_value is None:
self.cur.execute('INSERT INTO config VALUES(?, ?)', [key, default_value])
self.config[key] = default_value
else:
existing_value = existing_value[0]
if isinstance(default_value, int):
existing_value = int(existing_value)
self.config[key] = existing_value
def __repr__(self): def __repr__(self):
return 'TSDB(%s)' % self.filepath return 'TSDB(%s)' % self.filepath