From e06b58efaffbff7aab133369c3ac1b3f916672a7 Mon Sep 17 00:00:00 2001 From: Christian Speckner Date: Sat, 2 Jan 2021 16:06:44 +0100 Subject: [PATCH] Add a migration for an old settings DB (R77 style). --- src/common/repository/sqlite/SettingsDb.cxx | 24 +++++++++++++++---- .../repository/sqlite/SqliteDatabase.cxx | 7 ++---- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/common/repository/sqlite/SettingsDb.cxx b/src/common/repository/sqlite/SettingsDb.cxx index 986aa1e6d..910ea424a 100644 --- a/src/common/repository/sqlite/SettingsDb.cxx +++ b/src/common/repository/sqlite/SettingsDb.cxx @@ -25,7 +25,7 @@ #include "repository/CompositeKVRJsonAdapter.hxx" #include "repository/KeyValueRepositoryConfigfile.hxx" #include "KeyValueRepositorySqlite.hxx" -#include "SqliteTransaction.hxx" +#include "SqliteStatement.hxx" #include "FSNode.hxx" namespace { @@ -75,8 +75,6 @@ void SettingsDb::initialize() // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void SettingsDb::initializeDb() { - SqliteTransaction tx{*myDb}; - FilesystemNode legacyConfigFile{myDatabaseDirectory}; legacyConfigFile /= "stellarc"; @@ -88,10 +86,26 @@ void SettingsDb::initializeDb() { mySettingsRepository->save(KeyValueRepositoryConfigfile{legacyConfigFile}.load()); } + else if (legacyConfigDatabase.exists() && legacyConfigDatabase.isFile()) { + Logger::info("importing old settings from " + legacyConfigDatabase.getPath()); + + try { + SqliteStatement( + *myDb, + "ATTACH DATABASE ? AS old_db" + ) + .bind(1, legacyConfigDatabase.getPath()) + .step(); + + myDb->exec("INSERT INTO `settings` SELECT * FROM `old_db`.`settings`"); + myDb->exec("DETACH DATABASE `old_db`"); + } + catch (const SqliteError& err) { + Logger::error(err.what()); + } + } myDb->setUserVersion(CURRENT_VERSION); - - tx.commit(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/common/repository/sqlite/SqliteDatabase.cxx b/src/common/repository/sqlite/SqliteDatabase.cxx index d6ab3c350..3317261dc 100644 --- a/src/common/repository/sqlite/SqliteDatabase.cxx +++ b/src/common/repository/sqlite/SqliteDatabase.cxx @@ -97,7 +97,6 @@ void SqliteDatabase::exec(const string& sql) const Int32 SqliteDatabase::getUserVersion() const { SqliteStatement stmt(*this, "PRAGMA user_version"); - stmt.reset(); if (!stmt.step()) throw SqliteError("failed to get user_version"); @@ -108,8 +107,6 @@ Int32 SqliteDatabase::getUserVersion() const // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void SqliteDatabase::setUserVersion(Int32 version) const { - SqliteStatement stmt(*this, "PRAGMA user_version = %i", static_cast(version)); - stmt.reset(); - - stmt.step(); + SqliteStatement(*this, "PRAGMA user_version = %i", static_cast(version)) + .step(); }