Skip to content

Commit f4ca4f3

Browse files
committed
Simplify UPDATE on conflict
1 parent 7a8c3df commit f4ca4f3

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/chat.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3595,14 +3595,25 @@ pub(crate) async fn update_chat_contacts_table(
35953595
context
35963596
.sql
35973597
.transaction(move |transaction| {
3598-
transaction.execute("UPDATE chats_contacts
3598+
// Bump `remove_timestamp` to at least `now`
3599+
// even for members from `contacts`.
3600+
// We add members from `contacts` back below.
3601+
transaction.execute(
3602+
"UPDATE chats_contacts
35993603
SET remove_timestamp=MAX(add_timestamp+1, ?)
3600-
WHERE chat_id=?", (now, id))?;
3604+
WHERE chat_id=?",
3605+
(now, id),
3606+
)?;
3607+
36013608
for contact_id in contacts {
3609+
// We bumped `add_timestamp` for existing rows above,
3610+
// so on conflict it is enough to set `add_timestamp = remove_timestamp`
3611+
// and this guarantees that `add_timestamp` is no less than `now`.
36023612
transaction.execute(
3603-
"INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp) VALUES(?1, ?2, ?3)
3613+
"INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp)
3614+
VALUES (?1, ?2, ?3)
36043615
ON CONFLICT (chat_id, contact_id)
3605-
DO UPDATE SET add_timestamp=MAX(remove_timestamp, ?3)",
3616+
DO UPDATE SET add_timestamp=remove_timestamp",
36063617
(id, contact_id, now),
36073618
)?;
36083619
}

0 commit comments

Comments
 (0)