Skip to content

Commit 79add84

Browse files
Work around orphaned email attachments
1 parent 5cf3434 commit 79add84

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

src/db/email.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,23 @@ bool Database::sendEmail(EmailData* data, std::vector<sItemBase> attachments, Pl
329329
sqlite3_bind_int(stmt, 7, item.iTimeLimit);
330330

331331
if (sqlite3_step(stmt) != SQLITE_DONE) {
332-
std::cout << "[WARN] Database: Failed to send email: " << sqlite3_errmsg(db) << std::endl;
333-
sqlite3_exec(db, "ROLLBACK TRANSACTION;", NULL, NULL, NULL);
334-
sqlite3_finalize(stmt);
335-
return false;
332+
// very likely the UNIQUE constraint failing due to
333+
// orphaned attachments from an old email.
334+
// try deleting them first
335+
_deleteEmailAttachments(data->PlayerId, data->MsgIndex, -1);
336+
337+
// try again
338+
sqlite3_reset(stmt);
339+
if (sqlite3_step(stmt) != SQLITE_DONE) {
340+
// different error, give up
341+
std::cout << "[WARN] Database: Failed to send email: " << sqlite3_errmsg(db) << std::endl;
342+
sqlite3_exec(db, "ROLLBACK TRANSACTION;", NULL, NULL, NULL);
343+
sqlite3_finalize(stmt);
344+
return false;
345+
}
336346
}
337347
sqlite3_reset(stmt);
348+
sqlite3_clear_bindings(stmt);
338349
}
339350
sqlite3_finalize(stmt);
340351

0 commit comments

Comments
 (0)