Update timestamps in parameters with transactions

This avoids accidentally reverting the changes
to the `param` column done by another thread.
This commit is contained in:
link2xt
2023-02-22 14:32:22 +00:00
parent a82b09bfc2
commit d0a7e5f1b7
2 changed files with 42 additions and 14 deletions

View File

@@ -17,6 +17,7 @@
- Fix a problem with Gmail where (auto-)deleted messages would get archived instead of deleted. - Fix a problem with Gmail where (auto-)deleted messages would get archived instead of deleted.
Move them to the Trash folder for Gmail which auto-deletes trashed messages in 30 days #3972 Move them to the Trash folder for Gmail which auto-deletes trashed messages in 30 days #3972
- Clear config cache after backup import. This bug sometimes resulted in the import to seemingly work at first. #4067 - Clear config cache after backup import. This bug sometimes resulted in the import to seemingly work at first. #4067
- Update timestamps in `param` columns with transactions. #4083
### API-Changes ### API-Changes

View File

@@ -2,8 +2,8 @@
use anyhow::Result; use anyhow::Result;
use crate::chat::{Chat, ChatId}; use crate::chat::ChatId;
use crate::contact::{Contact, ContactId}; use crate::contact::ContactId;
use crate::context::Context; use crate::context::Context;
use crate::param::{Param, Params}; use crate::param::{Param, Params};
@@ -17,12 +17,26 @@ impl Context {
scope: Param, scope: Param,
new_timestamp: i64, new_timestamp: i64,
) -> Result<bool> { ) -> Result<bool> {
let mut contact = Contact::load_from_db(self, contact_id).await?; self.sql
if contact.param.update_timestamp(scope, new_timestamp)? { .transaction(|transaction| {
contact.update_param(self).await?; let mut param: Params = transaction.query_row(
return Ok(true); "SELECT param FROM contacts WHERE id=?",
} [contact_id],
Ok(false) |row| {
let param: String = row.get(0)?;
Ok(param.parse().unwrap_or_default())
},
)?;
let update = param.update_timestamp(scope, new_timestamp)?;
if update {
transaction.execute(
"UPDATE contacts SET param=? WHERE id=?",
params![param.to_string(), contact_id],
)?;
}
Ok(update)
})
.await
} }
} }
@@ -35,12 +49,24 @@ impl ChatId {
scope: Param, scope: Param,
new_timestamp: i64, new_timestamp: i64,
) -> Result<bool> { ) -> Result<bool> {
let mut chat = Chat::load_from_db(context, *self).await?; context
if chat.param.update_timestamp(scope, new_timestamp)? { .sql
chat.update_param(context).await?; .transaction(|transaction| {
return Ok(true); let mut param: Params =
} transaction.query_row("SELECT param FROM chats WHERE id=?", [self], |row| {
Ok(false) let param: String = row.get(0)?;
Ok(param.parse().unwrap_or_default())
})?;
let update = param.update_timestamp(scope, new_timestamp)?;
if update {
transaction.execute(
"UPDATE chats SET param=? WHERE id=?",
params![param.to_string(), self],
)?;
}
Ok(update)
})
.await
} }
} }
@@ -60,6 +86,7 @@ impl Params {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::chat::Chat;
use crate::receive_imf::receive_imf; use crate::receive_imf::receive_imf;
use crate::test_utils::TestContext; use crate::test_utils::TestContext;
use crate::tools::time; use crate::tools::time;