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 backedUpTablesBlocklist; 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 @@ -39,6 +39,14 @@ #ifndef EMSCRIPTEN NativeSQLiteConnectionManager SQLiteQueryExecutor::connectionManager; +std::unordered_set SQLiteQueryExecutor::backedUpTablesBlocklist = { + "olm_persist_account", + "olm_persist_sessions", + "metadata", + "messages_to_device", + "integrity_store", + "persist_storage", +}; #else SQLiteConnectionManager SQLiteQueryExecutor::connectionManager; #endif @@ -1090,6 +1098,8 @@ SQLiteQueryExecutor::SQLiteQueryExecutor() { SQLiteQueryExecutor::migrate(); #ifndef EMSCRIPTEN + SQLiteQueryExecutor::initializeTablesForLogMonitoring(); + std::string currentBackupID = this->getMetadata("backupID"); if (!StaffUtils::isStaffRelease() || !currentBackupID.size()) { return; @@ -2030,6 +2040,34 @@ }); } +void SQLiteQueryExecutor::initializeTablesForLogMonitoring() { + sqlite3 *db; + sqlite3_open(SQLiteQueryExecutor::sqliteFilePath.c_str(), &db); + default_on_db_open_callback(db); + + std::vector tablesToMonitor; + { + SQLiteStatementWrapper preparedSQL( + db, + "SELECT name FROM sqlite_master WHERE type='table';", + "Failed to get all database tables"); + + for (int stepResult = sqlite3_step(preparedSQL); stepResult == SQLITE_ROW; + stepResult = sqlite3_step(preparedSQL)) { + std::string table_name = + reinterpret_cast(sqlite3_column_text(preparedSQL, 0)); + if (SQLiteQueryExecutor::backedUpTablesBlocklist.find(table_name) == + SQLiteQueryExecutor::backedUpTablesBlocklist.end()) { + tablesToMonitor.emplace_back(table_name); + } + } + // Runs preparedSQL destructor which finalizes the sqlite statement + } + sqlite3_close(db); + + SQLiteQueryExecutor::connectionManager.tablesToMonitor = tablesToMonitor; +} + void SQLiteQueryExecutor::createMainCompaction(std::string backupID) const { std::string finalBackupPath = PlatformSpecificTools::getBackupFilePath(backupID, false); @@ -2086,12 +2124,15 @@ 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::backedUpTablesBlocklist.empty()) { + std::string removeDeviceSpecificDataSQL = ""; + for (const auto &table_name : + SQLiteQueryExecutor::backedUpTablesBlocklist) { + removeDeviceSpecificDataSQL.append("DELETE FROM " + table_name + ";\n"); + } + executeQuery(backupDB, removeDeviceSpecificDataSQL); + } + executeQuery(backupDB, "VACUUM;"); sqlite3_close(backupDB); diff --git a/web/shared-worker/_generated/comm_query_executor.wasm b/web/shared-worker/_generated/comm_query_executor.wasm index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 GIT binary patch literal 0 Hc$@