Skip to content
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
5 changes: 5 additions & 0 deletions dd.sql
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ declare
message_id bigint;
frozen boolean;
begin
-- A message is removed from this host only on purpose: a transaction that
-- sets fmsg.purge (SET LOCAL fmsg.purge = 'on') may delete its parts, so
-- the message can then be deleted. Once its row is gone, a reply to it is
-- rejected as parent not found.
if TG_OP='DELETE' and current_setting('fmsg.purge', true) = 'on' then return OLD; end if;
-- Delivery and read receipts do not change a protocol recipient.
if TG_OP='UPDATE' then
if TG_TABLE_NAME='msg_to' then
Expand Down
49 changes: 49 additions & 0 deletions pkg/message/store_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,55 @@ func TestFinalizeAndImmutability(t *testing.T) {
t.Fatal("committed sent message without hash")
}
}

// A purge transaction may remove a sent message and its parts; nothing else may.
func TestPurgeRemovesSentMessage(t *testing.T) {
db, _ := testStore(t)
root := insertDraft(t, db, nil, "root")
hash := seal(t, db, root)
child := insertDraft(t, db, root, "child")
seal(t, db, child)
if _, err := db.Exec(`INSERT INTO msg_attachment(msg_id,filename,filesize,filepath) VALUES($1,'a.txt',0,'')`, root); err == nil {
t.Fatal("attachment added to a sent message")
}
if _, err := db.Exec(`DELETE FROM msg_to WHERE msg_id=$1`, root); err == nil {
t.Fatal("deleted parts of a sent message without purging")
}

tx, err := db.Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
for _, q := range []string{
`SET LOCAL fmsg.purge = 'on'`,
`UPDATE msg SET pid = NULL WHERE pid = ` + fmt.Sprint(root),
`DELETE FROM msg_attachment WHERE msg_id = ` + fmt.Sprint(root),
`DELETE FROM msg_to WHERE msg_id = ` + fmt.Sprint(root),
`DELETE FROM msg WHERE id = ` + fmt.Sprint(root),
} {
if _, err := tx.Exec(q); err != nil {
t.Fatalf("%s: %v", q, err)
}
}
if err := tx.Commit(); err != nil {
t.Fatal(err)
}
var n int
if err := db.QueryRow(`SELECT count(*) FROM msg WHERE sha256 = $1`, hash).Scan(&n); err != nil || n != 0 {
t.Fatalf("purged message still found by hash: %d, %v", n, err)
}
// The reply survives, keeping the hash of the parent it was sent to.
var parent []byte
if err := db.QueryRow(`SELECT psha256 FROM msg WHERE id = $1 AND pid IS NULL`, child).Scan(&parent); err != nil || !bytes.Equal(parent, hash) {
t.Fatalf("reply after purge: %x, %v", parent, err)
}
// Outside a purge, the reply's parts stay immutable.
if _, err := db.Exec(`DELETE FROM msg_to WHERE msg_id=$1`, child); err == nil {
t.Fatal("deleted parts of a sent message after a purge transaction ended")
}
}

func TestFinalizeDraftBatchesAndRollback(t *testing.T) {
db, _ := testStore(t)
root := insertDraft(t, db, nil, "root with draft batch")
Expand Down
Loading