diff options
author | louiz’ <louiz@louiz.org> | 2017-06-21 17:36:53 +0200 |
---|---|---|
committer | louiz’ <louiz@louiz.org> | 2017-06-21 17:41:03 +0200 |
commit | 4d55a120d8fa5564a439102ab97b43da589bf4f3 (patch) | |
tree | c4b46b71ca41d933f348ab0a6683efd9f39c3044 | |
parent | 651caabb2d2f4e2d6bac3fcde59a69f9cad7eb75 (diff) | |
download | biboumi-4d55a120d8fa5564a439102ab97b43da589bf4f3.tar.gz biboumi-4d55a120d8fa5564a439102ab97b43da589bf4f3.tar.bz2 biboumi-4d55a120d8fa5564a439102ab97b43da589bf4f3.tar.xz biboumi-4d55a120d8fa5564a439102ab97b43da589bf4f3.zip |
Re-implement correctly the handling of failure to open the database
If we can’t open it at startup, we exit.
If we can’t open it on reload, we keep the previously-opened database.
This way, we’re assured to always have a valid and open database available.
-rw-r--r-- | src/database/database.cpp | 14 | ||||
-rw-r--r-- | src/utils/reload.cpp | 1 |
2 files changed, 12 insertions, 3 deletions
diff --git a/src/database/database.cpp b/src/database/database.cpp index cb41070..ba69d39 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -16,8 +16,18 @@ Database::IrcChannelOptionsTable Database::irc_channel_options("IrcChannelOption void Database::open(const std::string& filename) { - auto res = sqlite3_open_v2(filename.data(), &Database::db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr); - log_debug("open: ", res); + // Try to open the specified database. + // Close and replace the previous database pointer if it succeeded. If it did + // not, just leave things untouched + sqlite3* new_db; + auto res = sqlite3_open_v2(filename.data(), &new_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr); + if (res != SQLITE_OK) + { + log_error("Failed to open database file ", filename, ": ", sqlite3_errmsg(Database::db)); + throw std::runtime_error(""); + } + Database::close(); + Database::db = new_db; Database::muc_log_lines.create(Database::db); Database::muc_log_lines.upgrade(Database::db); Database::global_options.create(Database::db); diff --git a/src/utils/reload.cpp b/src/utils/reload.cpp index 807a9ab..fdca9bc 100644 --- a/src/utils/reload.cpp +++ b/src/utils/reload.cpp @@ -11,7 +11,6 @@ void open_database() #ifdef USE_DATABASE const auto db_filename = Config::get("db_name", xdg_data_path("biboumi.sqlite")); log_info("Opening database: ", db_filename); - Database::close(); Database::open(db_filename); log_info("database successfully opened."); #endif |