Add a migration for an old settings DB (R77 style).

This commit is contained in:
Christian Speckner 2021-01-02 16:06:44 +01:00
parent 2e25d20554
commit e06b58efaf
2 changed files with 21 additions and 10 deletions

View File

@ -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();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -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<int>(version));
stmt.reset();
stmt.step();
SqliteStatement(*this, "PRAGMA user_version = %i", static_cast<int>(version))
.step();
}