Merge pull request #1893 from H3rnand3zzz/fix/history-reliability

Improve logging of DB message insertion
This commit is contained in:
Michael Vetter
2023-10-15 00:40:40 +02:00
committed by GitHub

View File

@@ -380,7 +380,6 @@ _add_to_db(ProfMessage* message, char* type, const Jid* const from_jid, const Ji
} }
char* err_msg; char* err_msg;
gchar* query;
auto_gchar gchar* date_fmt; auto_gchar gchar* date_fmt;
if (message->timestamp) { if (message->timestamp) {
@@ -397,25 +396,49 @@ _add_to_db(ProfMessage* message, char* type, const Jid* const from_jid, const Ji
type = (char*)_get_message_type_str(message->type); type = (char*)_get_message_type_str(message->type);
} }
query = sqlite3_mprintf("INSERT INTO `ChatLogs` (`from_jid`, `from_resource`, `to_jid`, `to_resource`, `message`, `timestamp`, `stanza_id`, `archive_id`, `replace_id`, `type`, `encryption`) SELECT '%q', '%q', '%q', '%q', '%q', '%q', '%q', '%q', '%q', '%q', '%q' WHERE NOT EXISTS (SELECT 1 FROM `ChatLogs` WHERE (`archive_id` = '%q' AND `archive_id` != '') OR (`stanza_id` = '%q' AND `stanza_id` != ''))", auto_sqlite char* duplicate_check_query = sqlite3_mprintf("SELECT 1 FROM `ChatLogs` WHERE (`archive_id` = '%q' AND `archive_id` != '') OR (`stanza_id` = '%q' AND `stanza_id` != '')",
from_jid->barejid, message->stanzaid ? message->stanzaid : "",
from_jid->resourcepart ? from_jid->resourcepart : "", message->id ? message->id : "");
to_jid->barejid,
to_jid->resourcepart ? to_jid->resourcepart : "", if (!duplicate_check_query) {
message->plain ? message->plain : "", log_error("log_database_add(): SQL query for duplicate check. could not allocate memory");
date_fmt ? date_fmt : "", return;
message->id ? message->id : "", }
message->stanzaid ? message->stanzaid : "",
message->replace_id ? message->replace_id : "", int duplicate_exists = 0;
type ? type : "", sqlite3_stmt* stmt;
enc ? enc : "",
message->stanzaid ? message->stanzaid : "", if (SQLITE_OK == sqlite3_prepare_v2(g_chatlog_database, duplicate_check_query, -1, &stmt, NULL)) {
message->id ? message->id : ""); if (sqlite3_step(stmt) == SQLITE_ROW) {
duplicate_exists = 1;
}
sqlite3_finalize(stmt);
}
if (duplicate_exists) {
log_warning("Duplicate stanza-id found for the message. stanza_id: %s; archive_id: %s; sender: %s; content: %s", message->id, message->stanzaid, from_jid->barejid, message->plain);
return;
}
auto_sqlite char* query = sqlite3_mprintf("INSERT INTO `ChatLogs` (`from_jid`, `from_resource`, `to_jid`, `to_resource`, `message`, `timestamp`, `stanza_id`, `archive_id`, `replace_id`, `type`, `encryption`) VALUES ('%q', '%q', '%q', '%q', '%q', '%q', '%q', '%q', '%q', '%q', '%q')",
from_jid->barejid,
from_jid->resourcepart ? from_jid->resourcepart : "",
to_jid->barejid,
to_jid->resourcepart ? to_jid->resourcepart : "",
message->plain ? message->plain : "",
date_fmt ? date_fmt : "",
message->id ? message->id : "",
message->stanzaid ? message->stanzaid : "",
message->replace_id ? message->replace_id : "",
type ? type : "",
enc ? enc : "");
if (!query) { if (!query) {
log_error("log_database_add(): SQL query. could not allocate memory"); log_error("log_database_add(): SQL query. could not allocate memory");
return; return;
} }
log_debug("Writing to DB. Query: %s", query);
if (SQLITE_OK != sqlite3_exec(g_chatlog_database, query, NULL, 0, &err_msg)) { if (SQLITE_OK != sqlite3_exec(g_chatlog_database, query, NULL, 0, &err_msg)) {
if (err_msg) { if (err_msg) {
log_error("SQLite error: %s", err_msg); log_error("SQLite error: %s", err_msg);
@@ -423,6 +446,10 @@ _add_to_db(ProfMessage* message, char* type, const Jid* const from_jid, const Ji
} else { } else {
log_error("Unknown SQLite error"); log_error("Unknown SQLite error");
} }
} else {
int inserted_rows_count = sqlite3_changes(g_chatlog_database);
if (inserted_rows_count < 1) {
log_error("SQLite did not insert message (rows: %d, id: %s, content: %s)", inserted_rows_count, message->id, message->plain);
}
} }
sqlite3_free(query);
} }