Create _first_time_setup, _load_pragmas, and _check_version.
This commit is contained in:
parent
d982d64071
commit
0ef423d780
1 changed files with 34 additions and 13 deletions
|
@ -30,8 +30,16 @@ DB_FORMATS_USER = [
|
||||||
]
|
]
|
||||||
|
|
||||||
DATABASE_VERSION = 1
|
DATABASE_VERSION = 1
|
||||||
DB_INIT = '''
|
DB_VERSION_PRAGMA = f'''
|
||||||
PRAGMA user_version = {user_version};
|
PRAGMA user_version = {DATABASE_VERSION};
|
||||||
|
'''
|
||||||
|
|
||||||
|
DB_PRAGMAS = f'''
|
||||||
|
'''
|
||||||
|
|
||||||
|
DB_INIT = f'''
|
||||||
|
{DB_PRAGMAS}
|
||||||
|
{DB_VERSION_PRAGMA}
|
||||||
----------------------------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------------------------
|
||||||
CREATE TABLE IF NOT EXISTS config(
|
CREATE TABLE IF NOT EXISTS config(
|
||||||
key TEXT,
|
key TEXT,
|
||||||
|
@ -88,7 +96,7 @@ CREATE TABLE IF NOT EXISTS comment_edits(
|
||||||
replaced_at INT
|
replaced_at INT
|
||||||
);
|
);
|
||||||
CREATE INDEX IF NOT EXISTS comment_edits_index ON comment_edits(idstr);
|
CREATE INDEX IF NOT EXISTS comment_edits_index ON comment_edits(idstr);
|
||||||
'''.format(user_version=DATABASE_VERSION)
|
'''
|
||||||
|
|
||||||
DEFAULT_CONFIG = {
|
DEFAULT_CONFIG = {
|
||||||
'store_edits': True,
|
'store_edits': True,
|
||||||
|
@ -165,7 +173,7 @@ class DBEntry:
|
||||||
|
|
||||||
|
|
||||||
class TSDB:
|
class TSDB:
|
||||||
def __init__(self, filepath, do_create=True):
|
def __init__(self, filepath, *, do_create=True, skip_version_check=False):
|
||||||
self.filepath = pathclass.Path(filepath)
|
self.filepath = pathclass.Path(filepath)
|
||||||
if not self.filepath.is_file:
|
if not self.filepath.is_file:
|
||||||
if not do_create:
|
if not do_create:
|
||||||
|
@ -185,15 +193,11 @@ class TSDB:
|
||||||
self.cur = self.sql.cursor()
|
self.cur = self.sql.cursor()
|
||||||
|
|
||||||
if existing_database:
|
if existing_database:
|
||||||
self.cur.execute('PRAGMA user_version')
|
if not skip_version_check:
|
||||||
existing_version = self.cur.fetchone()[0]
|
self._check_version()
|
||||||
if existing_version > 0 and existing_version != DATABASE_VERSION:
|
self._load_pragmas()
|
||||||
raise exceptions.DatabaseOutOfDate(current=existing_version, new=DATABASE_VERSION)
|
else:
|
||||||
|
self._first_time_setup()
|
||||||
statements = DB_INIT.split(';')
|
|
||||||
for statement in statements:
|
|
||||||
self.cur.execute(statement)
|
|
||||||
self.sql.commit()
|
|
||||||
|
|
||||||
self.config = {}
|
self.config = {}
|
||||||
for (key, default_value) in DEFAULT_CONFIG.items():
|
for (key, default_value) in DEFAULT_CONFIG.items():
|
||||||
|
@ -208,6 +212,23 @@ class TSDB:
|
||||||
existing_value = int(existing_value)
|
existing_value = int(existing_value)
|
||||||
self.config[key] = existing_value
|
self.config[key] = existing_value
|
||||||
|
|
||||||
|
def _check_version(self):
|
||||||
|
'''
|
||||||
|
Compare database's user_version against constants.DATABASE_VERSION,
|
||||||
|
raising exceptions.DatabaseOutOfDate if not correct.
|
||||||
|
'''
|
||||||
|
existing = self.cur.execute('PRAGMA user_version').fetchone()[0]
|
||||||
|
if existing != DATABASE_VERSION:
|
||||||
|
raise exceptions.DatabaseOutOfDate(current=existing, new=DATABASE_VERSION)
|
||||||
|
|
||||||
|
def _first_time_setup(self):
|
||||||
|
self.sql.executescript(DB_INIT)
|
||||||
|
self.sql.commit()
|
||||||
|
|
||||||
|
def _load_pragmas(self):
|
||||||
|
self.sql.executescript(DB_PRAGMAS)
|
||||||
|
self.sql.commit()
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return 'TSDB(%s)' % self.filepath
|
return 'TSDB(%s)' % self.filepath
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue