Skip to content

Commit

Permalink
refactor: delete chat in a transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Nov 20, 2024
1 parent 5399cbf commit e121fc1
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,27 +765,19 @@ impl ChatId {
);

let chat = Chat::load_from_db(context, self).await?;
context
.sql
.execute(
"DELETE FROM msgs_mdns WHERE msg_id IN (SELECT id FROM msgs WHERE chat_id=?);",
(self,),
)
.await?;

context
.sql
.execute("DELETE FROM msgs WHERE chat_id=?;", (self,))
.await?;

context
.sql
.execute("DELETE FROM chats_contacts WHERE chat_id=?;", (self,))
.await?;

context
.sql
.execute("DELETE FROM chats WHERE id=?;", (self,))
.transaction(|transaction| {
transaction.execute(
"DELETE FROM msgs_mdns WHERE msg_id IN (SELECT id FROM msgs WHERE chat_id=?)",
(self,),
)?;
transaction.execute("DELETE FROM msgs WHERE chat_id=?", (self,))?;
transaction.execute("DELETE FROM chats_contacts WHERE chat_id=?", (self,))?;
transaction.execute("DELETE FROM chats WHERE id=?", (self,))?;
Ok(())
})
.await?;

context.emit_msgs_changed_without_ids();
Expand Down

0 comments on commit e121fc1

Please sign in to comment.