Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pins)_: remove pins when the og message is deleted #6231

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions protocol/message_persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
CREATE TABLE pin_messages_new (
id VARCHAR PRIMARY KEY NOT NULL,
message_id VARCHAR NOT NULL,
whisper_timestamp INTEGER NOT NULL,
chat_id VARCHAR NOT NULL,
local_chat_id VARCHAR NOT NULL,
clock_value INT NOT NULL,
pinned BOOLEAN NOT NULL,
pinned_by TEXT,
FOREIGN KEY (message_id) REFERENCES user_messages(id) ON DELETE CASCADE
);

INSERT INTO pin_messages_new (id, message_id, whisper_timestamp, chat_id, local_chat_id, clock_value, pinned, pinned_by)
SELECT id, message_id, whisper_timestamp, chat_id, local_chat_id, clock_value, pinned, pinned_by
FROM pin_messages;

DROP TABLE pin_messages;

ALTER TABLE pin_messages_new RENAME TO pin_messages;
-- Leave this migration empty
-- This used to be a migration to add ON DELETE CASCADE to the pinned_messages table
-- However, we found that it had the side effect of preventing pinned messages to arrive before the messages they are pinned to
-- Leaving this migration here to prevent the migration from being run again
-- People that ran the migraiton already will have the wrong behavior on edge cases, but we can't fix that easily
-- People that didn't run this migration will have the correct behavior
-- This was never run on a release
-- See more info here: https://github.com/status-im/status-go/pull/6231#discussion_r1904038990
Loading