Start tracking database user_version for potential upgrades.

master
Ethan Dalool 2017-11-15 20:22:29 -08:00
parent 708c774e52
commit 2b71ec3c76
1 changed files with 17 additions and 0 deletions

View File

@ -26,7 +26,10 @@ DB_FORMATS_USER = [
'.\\users\\@{name}\\@{name}.db', '.\\users\\@{name}\\@{name}.db',
] ]
DATABASE_VERSION = 1
DB_INIT = ''' DB_INIT = '''
PRAGMA user_version = {user_version};
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS submissions( CREATE TABLE IF NOT EXISTS submissions(
idint INT, idint INT,
idstr TEXT, idstr TEXT,
@ -63,6 +66,10 @@ CREATE TABLE IF NOT EXISTS comments(
textlen INT textlen INT
); );
CREATE INDEX IF NOT EXISTS comment_index ON comments(idstr); CREATE INDEX IF NOT EXISTS comment_index ON comments(idstr);
'''.format(user_version=DATABASE_VERSION)
ERROR_DATABASE_OUTOFDATE = '''
Database is out of date. {current} should be {new}.
'''.strip() '''.strip()
SQL_SUBMISSION_COLUMNS = [ SQL_SUBMISSION_COLUMNS = [
@ -120,8 +127,18 @@ class TSDB:
self.styles_dir = self.filepath.parent.with_child('styles') self.styles_dir = self.filepath.parent.with_child('styles')
self.wiki_dir = self.filepath.parent.with_child('wiki') self.wiki_dir = self.filepath.parent.with_child('wiki')
existing_database = self.filepath.exists
self.sql = sqlite3.connect(self.filepath.absolute_path) self.sql = sqlite3.connect(self.filepath.absolute_path)
self.cur = self.sql.cursor() self.cur = self.sql.cursor()
if existing_database:
self.cur.execute('PRAGMA user_version')
existing_version = self.cur.fetchone()[0]
if existing_version > 0 and existing_version != DATABASE_VERSION:
message = ERROR_DATABASE_OUTOFDATE
message = message.format(current=existing_version, new=DATABASE_VERSION)
raise ValueError(message)
statements = DB_INIT.split(';') statements = DB_INIT.split(';')
for statement in statements: for statement in statements:
self.cur.execute(statement) self.cur.execute(statement)