From ea1864a3e4e5b879a9ae84d3d97c23eebb0dc77a Mon Sep 17 00:00:00 2001 From: kaliko Date: Tue, 4 May 2021 20:21:13 +0200 Subject: [PATCH] Add new database replacement code. In case a previous database is loaded, it is renamed with a prefix "-old-version-backup" in the same directory. No migration managed. --- sima/launch.py | 9 ++++++++- sima/lib/simadb.py | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/sima/launch.py b/sima/launch.py index 27e9d6b..890f96b 100644 --- a/sima/launch.py +++ b/sima/launch.py @@ -26,6 +26,7 @@ import sys from importlib import __import__ as sima_import from os.path import isfile +from os import rename ## # third parties components @@ -92,7 +93,13 @@ def start(sopt, restart=False): db_file = config.get('sima', 'db_file') if not isfile(db_file): logger.debug('Creating database in "%s"', db_file) - open(db_file, 'a').close() # TODO: to remove with new simadb in v0.18 + SimaDB(db_path=db_file).create_db() + # Migration from v0.17.0 + dbinfo = SimaDB(db_path=db_file).get_info() + if not dbinfo: # v0.17.0 → v0.18+ migration + logger.warning('Backing up database!') + rename(db_file, db_file + '-old-version-backup') + logger.info('Creating an new database in "%s"', db_file) SimaDB(db_path=db_file).create_db() if sopt.options.get('command'): diff --git a/sima/lib/simadb.py b/sima/lib/simadb.py index ca2277d..60844a2 100644 --- a/sima/lib/simadb.py +++ b/sima/lib/simadb.py @@ -49,6 +49,13 @@ class SimaDB: self._db_path, isolation_level=None) return connection + def get_info(self): + connection = self.get_database_connection() + info = connection.execute("""SELECT * FROM db_info + WHERE name = "DB Version" LIMIT 1;""").fetchone() + connection.close() + return info + def create_db(self): """ Set up a database """ -- 2.39.5