From 9ff27483ab6ff7bfa67244a84184768980a8d8ed Mon Sep 17 00:00:00 2001 From: faumaray Date: Mon, 22 Jun 2026 16:40:55 +0300 Subject: [PATCH] Own the database connection via Void::Sqlite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DatabaseImpl now holds a Void::Sqlite::Database (RAII open/close, WAL + busy-timeout + foreign_keys); the existing raw-sqlite3 query methods borrow its handle() unchanged. Removes the manual sqlite3_open/close + journal-mode boilerplate. Deliberately keeps echoion's schema_migrations migration framework rather than switching to Void's PRAGMA user_version Migrator: existing databases record versions in schema_migrations with user_version=0, and migrations 2/3 DROP tables — re-running them would destroy live data. Verified with an integration test (data survives close+reopen; migrate() stays idempotent). Link Void::Sqlite. Co-Authored-By: Claude Opus 4.8 --- CMakeLists.txt | 1 + src/Database.cpp | 45 +++++++++++++++++++++++++-------------------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d07387..f1dc645 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -106,6 +106,7 @@ target_link_libraries(EchoionCore Void::Time Void::Math Void::Net + Void::Sqlite PRIVATE Threads::Threads qjs diff --git a/src/Database.cpp b/src/Database.cpp index 11cf49c..5c53244 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -1,6 +1,7 @@ #include #include "echoion/core/Logger.hpp" #include +#include #include #include #include @@ -9,12 +10,20 @@ namespace Echoion::Api { - // Internal implementation + // Internal implementation. The connection lifecycle (open/close, WAL, + // busy-timeout, foreign keys) is owned by Void::Sqlite::Database; `db` is a + // non-owning handle into it so the existing raw-sqlite3 query methods and + // the schema_migrations framework keep working byte-identically. The + // migration mechanism is deliberately NOT switched to Void's user_version + // Migrator: existing databases track versions in schema_migrations with + // user_version=0, and migrations 2/3 DROP tables — re-running them would + // destroy live data. class DatabaseImpl { public: - sqlite3* db = nullptr; + Void::Sqlite::Database voidDb; + sqlite3* db = nullptr; // non-owning, == voidDb.handle() DatabaseConfig config; bool initialized = false; int version = 0; @@ -36,25 +45,21 @@ namespace Echoion::Api impl_->config = config; - int rc = sqlite3_open(config.path.c_str(), &impl_->db); - if (rc != SQLITE_OK) { + // Open via Void::Sqlite (RAII; sets WAL + busy_timeout + foreign_keys + // for read-write modes). The raw handle is borrowed for the existing + // query methods. + auto opened = Void::Sqlite::Database::open( + config.path, Void::Sqlite::OpenMode::ReadWriteCreate); + if (!opened) { return std::unexpected( Error(ErrorCode::DatabaseError, std::format("Failed to open database: {}", - sqlite3_errmsg(impl_->db)))); + opened.error().message()))); } + impl_->voidDb = std::move(*opened); + impl_->db = impl_->voidDb.handle(); - // Enable WAL mode for better concurrency - if (config.walMode) { - char* errMsg = nullptr; - sqlite3_exec(impl_->db, "PRAGMA journal_mode=WAL;", nullptr, - nullptr, &errMsg); - if (errMsg) { - sqlite3_free(errMsg); - } - } - - // Set busy timeout + // Honor the configured busy timeout (Void defaults to 5s). sqlite3_busy_timeout(impl_->db, static_cast(config.busyTimeout.count())); @@ -83,10 +88,10 @@ namespace Echoion::Api std::unique_lock lock(mutex_); - if (impl_->db) { - sqlite3_close(impl_->db); - impl_->db = nullptr; - } + // Drop the borrowed handle first, then let Void::Sqlite close the + // connection via RAII (move-assign an empty Database). + impl_->db = nullptr; + impl_->voidDb = Void::Sqlite::Database{}; impl_->initialized = false; }