From 72a72bc0abe458bda995aa4ec5622c958018e9ab Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 10 Jan 2025 11:26:56 -0500 Subject: [PATCH] fix(pins)_: remove pins when the original message is deleted Fixes #6246 --- protocol/message_persistence.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/protocol/message_persistence.go b/protocol/message_persistence.go index 10fc15a5b5..4332ea118b 100644 --- a/protocol/message_persistence.go +++ b/protocol/message_persistence.go @@ -1768,6 +1768,13 @@ func (db sqlitePersistence) SavePinMessage(message *common.PinMessage) (inserted func (db sqlitePersistence) DeleteMessage(id string) error { _, err := db.db.Exec(`DELETE FROM user_messages WHERE id = ?`, id) + + if err != nil { + return err + } + + _, err = db.db.Exec("DELETE FROM pin_messages WHERE message_id = ?", id) + return err } @@ -1778,7 +1785,25 @@ func (db sqlitePersistence) DeleteMessages(ids []string) error { } inVector := strings.Repeat("?, ", len(ids)-1) + "?" - _, err := db.db.Exec("DELETE FROM user_messages WHERE id IN ("+inVector+")", idsArgs...) // nolint: gosec + tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{}) + if err != nil { + return err + } + defer func() { + if err == nil { + err = tx.Commit() + return + } + // don't shadow original error + _ = tx.Rollback() + }() + + _, err = tx.Exec("DELETE FROM user_messages WHERE id IN ("+inVector+")", idsArgs...) // nolint: gosec + if err != nil { + return err + } + + _, err = tx.Exec("DELETE FROM pin_messages WHERE message_id IN ("+inVector+")", idsArgs...) // nolint: gosec return err }