diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.h b/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.h --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.h +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -7,5 +8,7 @@ class SQLiteBackup { public: static std::unordered_set tablesAllowlist; + + static void cleanupDatabaseExceptAllowlist(sqlite3 *db); }; } // namespace comm diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.cpp @@ -1,7 +1,13 @@ #include "SQLiteBackup.h" +#include "SQLiteUtils.h" +#include "entities/EntityQueryHelpers.h" + +#include +#include #include #include +#include namespace comm { @@ -15,4 +21,22 @@ "entries", }; +void SQLiteBackup::cleanupDatabaseExceptAllowlist(sqlite3 *db) { + std::vector tables = SQLiteUtils::getAllTableNames(db); + + std::ostringstream removeDeviceSpecificDataSQL; + for (const auto &tableName : tables) { + if (SQLiteBackup::tablesAllowlist.find(tableName) == + SQLiteBackup::tablesAllowlist.end()) { + removeDeviceSpecificDataSQL << "DELETE FROM " << tableName << ";" + << std::endl; + } + } + + std::string sqlQuery = removeDeviceSpecificDataSQL.str(); + if (!sqlQuery.empty()) { + executeQuery(db, sqlQuery); + } +} + } // namespace comm 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 @@ -25,15 +25,10 @@ class SQLiteQueryExecutor : public DatabaseQueryExecutor { static void closeConnection(); -#ifndef EMSCRIPTEN - void cleanupDatabaseExceptAllowlist(sqlite3 *db) const; -#endif - std::optional getSyncedDatabaseVersion(sqlite3 *db) const; std::vector processMessagesResults(SQLiteStatementWrapper &preparedSQL) const; std::string getThickThreadTypesList() const; - std::vector getAllTableNames(sqlite3 *db) const; void copyTablesDataUsingAttach( sqlite3 *db, const std::string &sourceDbPath, 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 @@ -1629,28 +1629,6 @@ SQLiteQueryExecutor::getConnection(), query, types); } -std::vector -SQLiteQueryExecutor::getAllTableNames(sqlite3 *db) const { - std::vector tableNames; - - std::string getAllTablesQuery = - "SELECT name " - "FROM sqlite_master" - "WHERE type='table';"; - - sqlite3_stmt *stmt; - sqlite3_prepare_v2(db, getAllTablesQuery.c_str(), -1, &stmt, nullptr); - while (sqlite3_step(stmt) == SQLITE_ROW) { - const unsigned char *text = sqlite3_column_text(stmt, 0); - if (text) { - tableNames.push_back(reinterpret_cast(text)); - } - } - sqlite3_finalize(stmt); - - return tableNames; -} - #ifndef EMSCRIPTEN void SQLiteQueryExecutor::initialize( @@ -1662,24 +1640,6 @@ SQLiteQueryExecutor::backupLogDataKey = backupLogDataKey; } -void SQLiteQueryExecutor::cleanupDatabaseExceptAllowlist(sqlite3 *db) const { - std::vector tables = getAllTableNames(db); - - std::ostringstream removeDeviceSpecificDataSQL; - for (const auto &tableName : tables) { - if (SQLiteBackup::tablesAllowlist.find(tableName) == - SQLiteBackup::tablesAllowlist.end()) { - removeDeviceSpecificDataSQL << "DELETE FROM " << tableName << ";" - << std::endl; - } - } - - std::string sqlQuery = removeDeviceSpecificDataSQL.str(); - if (!sqlQuery.empty()) { - executeQuery(db, sqlQuery); - } -} - void SQLiteQueryExecutor::createMainCompaction(std::string backupID) const { std::string finalBackupPath = PlatformSpecificTools::getBackupFilePath(backupID, false); @@ -1740,7 +1700,7 @@ throw std::runtime_error(error_message.str()); } - cleanupDatabaseExceptAllowlist(backupDB); + SQLiteBackup::cleanupDatabaseExceptAllowlist(backupDB); executeQuery(backupDB, "VACUUM;"); sqlite3_close(backupDB); diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.h b/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.h --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.h +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.h @@ -28,6 +28,7 @@ const std::string &sqliteFilePath, const std::string &encryptionKey); static void rekeyDatabase(sqlite3 *db, const std::string &encryptionKey); + static std::vector getAllTableNames(sqlite3 *db); }; } // namespace comm diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.cpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace comm { @@ -207,4 +208,25 @@ executeQuery(db, rekey_encryption_key_query); } +std::vector SQLiteUtils::getAllTableNames(sqlite3 *db) { + std::vector tableNames; + + std::string getAllTablesQuery = + "SELECT name " + "FROM sqlite_master" + "WHERE type='table';"; + + sqlite3_stmt *stmt; + sqlite3_prepare_v2(db, getAllTablesQuery.c_str(), -1, &stmt, nullptr); + while (sqlite3_step(stmt) == SQLITE_ROW) { + const unsigned char *text = sqlite3_column_text(stmt, 0); + if (text) { + tableNames.push_back(reinterpret_cast(text)); + } + } + sqlite3_finalize(stmt); + + return tableNames; +} + } // namespace comm 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$@