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 @@ -301,10 +301,31 @@ } void SQLiteQueryExecutor::replaceMessage(const Message &message) const { + int sidebarSourceTypeInt = static_cast(MessageType::SIDEBAR_SOURCE); + std::string sidebarSourceType = std::to_string(sidebarSourceTypeInt); + static std::string replaceMessageSQL = - "REPLACE INTO messages " - "(id, local_id, thread, user, type, future_type, content, time) " - "VALUES (?, ?, ?, ?, ?, ?, ?, ?);"; + "REPLACE INTO messages (" + " id, local_id, thread, user, type, future_type, content, time, " + " target_message" + ")" + "VALUES ( " + " :id, :local_id, :thread, :user, :type, :future_type, :content, :time," + " IIF(" + " JSON_VALID(:content)," + " COALESCE(" + " JSON_EXTRACT(:content, '$.targetMessageID')," + " IIF(" + " :type = " + + sidebarSourceType + + "," + " JSON_EXTRACT(:content, '$.id')," + " NULL" + " )" + " )," + " NULL" + " )" + ");"; replaceEntity(this->getConnection(), replaceMessageSQL, message); } diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteSchema.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteSchema.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteSchema.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteSchema.cpp @@ -1,6 +1,5 @@ #include "SQLiteSchema.h" -#include "../NativeModules/PersistentStorageUtilities/MessageOperationsUtilities/MessageTypeEnum.h" #include "Logger.h" #include "SQLiteUtils.h" @@ -13,10 +12,7 @@ namespace comm { bool SQLiteSchema::createSchema(sqlite3 *db) { - char *error; - int sidebarSourceTypeInt = static_cast(MessageType::SIDEBAR_SOURCE); - std::string sidebarSourceType = std::to_string(sidebarSourceTypeInt); - auto query = + std::string query = "CREATE TABLE IF NOT EXISTS drafts (" " key TEXT UNIQUE PRIMARY KEY NOT NULL," " text TEXT NOT NULL" @@ -31,21 +27,7 @@ " future_type INTEGER," " content TEXT," " time INTEGER NOT NULL," - " target_message TEXT AS (" - " IIF(" - " JSON_VALID(content)," - " COALESCE(" - " JSON_EXTRACT(content, '$.targetMessageID')," - " IIF(" - " type = " + - sidebarSourceType + - " , JSON_EXTRACT(content, '$.id')," - " NULL" - " )" - " )," - " NULL" - " )" - " )" + " target_message TEXT" ");" "CREATE TABLE IF NOT EXISTS olm_persist_account (" @@ -207,6 +189,7 @@ "CREATE INDEX IF NOT EXISTS dm_operations_idx_type" " ON dm_operations (type);"; + char *error; sqlite3_exec(db, query.c_str(), nullptr, nullptr, &error); if (!error) { diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteSchemaMigrations.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteSchemaMigrations.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteSchemaMigrations.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteSchemaMigrations.cpp @@ -778,6 +778,49 @@ return SQLiteSchema::createTable(db, query, "dm_operations"); } +bool convert_target_message_to_standard_column(sqlite3 *db) { + int sidebarSourceTypeInt = static_cast(MessageType::SIDEBAR_SOURCE); + std::string sidebarSourceType = std::to_string(sidebarSourceTypeInt); + + std::string query = + "DROP INDEX IF EXISTS messages_idx_target_message_type_time;" + "ALTER TABLE messages DROP COLUMN target_message;" + "ALTER TABLE messages ADD COLUMN target_message TEXT;" + "UPDATE messages SET target_message = (" + " IIF(" + " JSON_VALID(content)," + " COALESCE(" + " JSON_EXTRACT(content, '$.targetMessageID')," + " IIF(" + " type = " + + sidebarSourceType + + "," + " JSON_EXTRACT(content, '$.id')," + " NULL" + " )" + " )," + " NULL" + " )" + ");" + "CREATE INDEX IF NOT EXISTS messages_idx_target_message_type_time " + " ON messages (target_message, type, time);"; + + char *error; + sqlite3_exec(db, query.c_str(), nullptr, nullptr, &error); + + if (!error) { + return true; + } + + std::ostringstream stringStream; + stringStream << "Error converting target_message to standard column: " + << error; + Logger::log(stringStream.str()); + + sqlite3_free(error); + return false; +} + SQLiteMigrations SQLiteSchema::migrations{ {{1, {create_drafts_table, true}}, {2, {rename_threadID_to_key, true}}, @@ -822,6 +865,7 @@ {51, {update_messages_idx_target_message_type_time, true}}, {52, {recreate_inbound_p2p_messages_table, true}}, {53, {add_timestamps_column_to_threads_table, true}}, - {54, {create_dm_operations_table, true}}}}; + {54, {create_dm_operations_table, true}}, + {55, {convert_target_message_to_standard_column, true}}}}; } // namespace comm diff --git a/native/cpp/CommonCpp/DatabaseManagers/entities/Message.h b/native/cpp/CommonCpp/DatabaseManagers/entities/Message.h --- a/native/cpp/CommonCpp/DatabaseManagers/entities/Message.h +++ b/native/cpp/CommonCpp/DatabaseManagers/entities/Message.h @@ -32,14 +32,39 @@ } int bindToSQL(sqlite3_stmt *sql, int idx) const { - bindStringToSQL(id, sql, idx); - bindOptionalStringToSQL(local_id, sql, idx + 1); - bindStringToSQL(thread, sql, idx + 2); - bindStringToSQL(user, sql, idx + 3); - bindIntToSQL(type, sql, idx + 4); - bindOptionalIntToSQL(future_type, sql, idx + 5); - bindOptionalStringToSQL(content, sql, idx + 6); - return bindInt64ToSQL(time, sql, idx + 7); + int err; + + int id_index = sqlite3_bind_parameter_index(sql, ":id"); + err = bindStringToSQL(id, sql, id_index); + + int local_id_index = sqlite3_bind_parameter_index(sql, ":local_id"); + if (local_id_index) { + err = bindOptionalStringToSQL(local_id, sql, local_id_index); + } + + int thread_index = sqlite3_bind_parameter_index(sql, ":thread"); + err = bindStringToSQL(thread, sql, thread_index); + + int user_index = sqlite3_bind_parameter_index(sql, ":user"); + err = bindStringToSQL(user, sql, user_index); + + int type_index = sqlite3_bind_parameter_index(sql, ":type"); + err = bindIntToSQL(type, sql, type_index); + + int future_type_index = sqlite3_bind_parameter_index(sql, ":future_type"); + if (future_type_index) { + err = bindOptionalIntToSQL(future_type, sql, future_type_index); + } + + int content_index = sqlite3_bind_parameter_index(sql, ":content"); + if (content_index) { + err = bindOptionalStringToSQL(content, sql, content_index); + } + + int time_index = sqlite3_bind_parameter_index(sql, ":time"); + err = bindInt64ToSQL(time, sql, time_index); + + return err; } }; 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$@