diff --git a/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.h b/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.h --- a/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.h +++ b/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.h @@ -19,6 +19,7 @@ getAttachmentsFromLog(std::uint8_t *patchsetPtr, int patchsetSize); public: + std::vector tablesToMonitor; NativeSQLiteConnectionManager(); void setLogsMonitoring(bool enabled); bool getLogsMonitoring(); diff --git a/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.cpp b/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/NativeSQLiteConnectionManager.cpp @@ -20,16 +20,6 @@ sqlite3session_create(dbConnection, "main", &backupLogsSession); handleSQLiteError(sessionCreationResult, "Failed to create sqlite3 session."); - static const std::vector tablesToMonitor = { - "drafts", - "messages", - "media", - "threads", - "message_store_threads", - "reports", - "keyservers", - "users"}; - for (const auto &table : tablesToMonitor) { int sessionAttachResult = sqlite3session_attach(backupLogsSession, table.c_str()); diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h @@ -11,6 +11,7 @@ #include #include +#include namespace comm { @@ -28,8 +29,10 @@ #ifndef EMSCRIPTEN static NativeSQLiteConnectionManager connectionManager; + static std::unordered_set backedUpTableBlocklist; static void generateFreshEncryptionKey(); static void generateFreshBackupLogsEncryptionKey(); + static void initializeTablesForLogMonitoring(); #else static SQLiteConnectionManager connectionManager; #endif diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp @@ -38,6 +38,14 @@ #ifndef EMSCRIPTEN NativeSQLiteConnectionManager SQLiteQueryExecutor::connectionManager; +std::unordered_set SQLiteQueryExecutor::backedUpTableBlocklist = { + "olm_persist_account", + "olm_persist_sessions", + "metadata", + "messages_to_device", + "integrity_store", + "persist_storage", +}; #else SQLiteConnectionManager SQLiteQueryExecutor::connectionManager; #endif @@ -1071,9 +1079,42 @@ sqlite3_close(db); } +#ifndef EMSCRIPTEN +void SQLiteQueryExecutor::initializeTablesForLogMonitoring() { + sqlite3 *db; + sqlite3_open(SQLiteQueryExecutor::sqliteFilePath.c_str(), &db); + default_on_db_open_callback(db); + + std::vector tablesToMonitor; + + sqlite3_stmt *table_names_stm; + sqlite3_prepare_v2( + db, + "SELECT name FROM sqlite_master WHERE type='table';", + -1, + &table_names_stm, + nullptr); + for (int stepResult = sqlite3_step(table_names_stm); stepResult == SQLITE_ROW; + stepResult = sqlite3_step(table_names_stm)) { + std::string table_name = + reinterpret_cast(sqlite3_column_text(table_names_stm, 0)); + if (SQLiteQueryExecutor::backedUpTableBlocklist.find(table_name) == + SQLiteQueryExecutor::backedUpTableBlocklist.end()) { + tablesToMonitor.emplace_back(table_name); + } + } + sqlite3_finalize(table_names_stm); + sqlite3_close(db); + + SQLiteQueryExecutor::connectionManager.tablesToMonitor = tablesToMonitor; +} +#endif + SQLiteQueryExecutor::SQLiteQueryExecutor() { SQLiteQueryExecutor::migrate(); #ifndef EMSCRIPTEN + SQLiteQueryExecutor::initializeTablesForLogMonitoring(); + std::string currentBackupID = this->getMetadata("backupID"); if (!StaffUtils::isStaffRelease() || !currentBackupID.size()) { return; @@ -2025,12 +2066,14 @@ throw std::runtime_error(error_message.str()); } - std::string removeDeviceSpecificDataSQL = - "DELETE FROM olm_persist_account;" - "DELETE FROM olm_persist_sessions;" - "DELETE FROM metadata;" - "DELETE FROM messages_to_device;"; - executeQuery(backupDB, removeDeviceSpecificDataSQL); + if (!SQLiteQueryExecutor::backedUpTableBlocklist.empty()) { + std::string removeDeviceSpecificDataSQL = ""; + for (const auto &table_name : SQLiteQueryExecutor::backedUpTableBlocklist) { + removeDeviceSpecificDataSQL.append("DELETE FROM " + table_name + ";\n"); + } + executeQuery(backupDB, removeDeviceSpecificDataSQL); + } + executeQuery(backupDB, "VACUUM;"); sqlite3_close(backupDB);