From a0a0075e4d38256cb6749119458432edb6f972ff 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 | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/protocol/message_persistence.go b/protocol/message_persistence.go index 10fc15a5b5..017ded4683 100644 --- a/protocol/message_persistence.go +++ b/protocol/message_persistence.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "encoding/json" + "errors" "fmt" "sort" "strings" @@ -1768,17 +1769,41 @@ 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 } -func (db sqlitePersistence) DeleteMessages(ids []string) error { +func (db sqlitePersistence) DeleteMessages(ids []string) (err error) { idsArgs := make([]interface{}, 0, len(ids)) for _, id := range ids { idsArgs = append(idsArgs, id) } 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 + } + err = errors.Join(err, 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 }