DB v2. Rename redmash folder to index.

master
Ethan Dalool 2020-01-27 19:53:27 -08:00
parent 0ef423d780
commit d12e66f19c
2 changed files with 25 additions and 11 deletions

View File

@ -29,7 +29,7 @@ DB_FORMATS_USER = [
'.\\users\\@{name}\\@{name}.db', '.\\users\\@{name}\\@{name}.db',
] ]
DATABASE_VERSION = 1 DATABASE_VERSION = 2
DB_VERSION_PRAGMA = f''' DB_VERSION_PRAGMA = f'''
PRAGMA user_version = {DATABASE_VERSION}; PRAGMA user_version = {DATABASE_VERSION};
''' '''
@ -184,7 +184,7 @@ class TSDB:
self.breakdown_dir = self.filepath.parent.with_child('breakdown') self.breakdown_dir = self.filepath.parent.with_child('breakdown')
self.offline_reading_dir = self.filepath.parent.with_child('offline_reading') self.offline_reading_dir = self.filepath.parent.with_child('offline_reading')
self.index_dir = self.filepath.parent.with_child('redmash') self.index_dir = self.filepath.parent.with_child('index')
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')

View File

@ -3,23 +3,37 @@ import os
import sqlite3 import sqlite3
import sys import sys
import timesearch_modules.tsdb sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from timesearch_modules import tsdb
def upgrade_1_to_2(db):
'''
In this version, many of the timesearch modules were renamed, including
redmash -> index. This update will rename the existing `redmash` folder
to `index`.
'''
cur = db.sql.cursor()
redmash_dir = db.index_dir.parent.with_child('redmash')
if redmash_dir.exists:
if not redmash_dir.is_dir:
raise Exception(f'{redmash_dir.absolute_path} is not a directory!')
print('Renaming redmash folder to index.')
os.rename(redmash_dir.absolute_path, db.index_dir.absolute_path)
def upgrade_all(database_filename): def upgrade_all(database_filename):
''' '''
Given the filename of a database, apply all of the needed Given the filename of a database, apply all of the needed
upgrade_x_to_y functions in order. upgrade_x_to_y functions in order.
''' '''
if not os.path.isfile(database_filename): db = tsdb.TSDB(database_filename, do_create=False, skip_version_check=True)
raise FileNotFoundError(database_filename)
sql = sqlite3.connect(database_filename) cur = db.sql.cursor()
cur = sql.cursor()
cur.execute('PRAGMA user_version') cur.execute('PRAGMA user_version')
current_version = cur.fetchone()[0] current_version = cur.fetchone()[0]
needed_version = timesearch_modules.tsdb.DATABASE_VERSION needed_version = tsdb.DATABASE_VERSION
if current_version == needed_version: if current_version == needed_version:
print('Already up to date with version %d.' % needed_version) print('Already up to date with version %d.' % needed_version)
@ -29,9 +43,9 @@ def upgrade_all(database_filename):
print('Upgrading from %d to %d' % (current_version, version_number)) print('Upgrading from %d to %d' % (current_version, version_number))
upgrade_function = 'upgrade_%d_to_%d' % (current_version, version_number) upgrade_function = 'upgrade_%d_to_%d' % (current_version, version_number)
upgrade_function = eval(upgrade_function) upgrade_function = eval(upgrade_function)
upgrade_function(sql) upgrade_function(db)
sql.cursor().execute('PRAGMA user_version = %d' % version_number) db.sql.cursor().execute('PRAGMA user_version = %d' % version_number)
sql.commit() db.sql.commit()
current_version = version_number current_version = version_number
print('Upgrades finished.') print('Upgrades finished.')