Correctly clear database cache after import (#4067)

This commit is contained in:
Hocuri
2023-02-21 12:23:34 +01:00
committed by GitHub
parent 05a274a5f3
commit 17acbca576
3 changed files with 84 additions and 36 deletions

View File

@@ -13,6 +13,7 @@
- Start SQL transactions with IMMEDIATE behaviour rather than default DEFERRED one. #4063 - Start SQL transactions with IMMEDIATE behaviour rather than default DEFERRED one. #4063
- 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
### API-Changes ### API-Changes

View File

@@ -427,8 +427,6 @@ async fn import_backup(
context.get_dbfile().display() context.get_dbfile().display()
); );
context.sql.config_cache.write().await.clear();
let mut archive = Archive::new(backup_file); let mut archive = Archive::new(backup_file);
let mut entries = archive.entries()?; let mut entries = archive.entries()?;
@@ -785,7 +783,10 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::time::Duration;
use ::pgp::armor::BlockType; use ::pgp::armor::BlockType;
use tokio::task;
use super::*; use super::*;
use crate::pgp::{split_armored_data, HEADER_AUTOCRYPT, HEADER_SETUPCODE}; use crate::pgp::{split_armored_data, HEADER_AUTOCRYPT, HEADER_SETUPCODE};
@@ -933,6 +934,46 @@ mod tests {
Ok(()) Ok(())
} }
/// This is a regression test for
/// https://github.com/deltachat/deltachat-android/issues/2263
/// where the config cache wasn't reset properly after a backup.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_import_backup_reset_config_cache() -> Result<()> {
let backup_dir = tempfile::tempdir()?;
let context1 = TestContext::new_alice().await;
let context2 = TestContext::new().await;
assert!(!context2.is_configured().await?);
// export from context1
imex(&context1, ImexMode::ExportBackup, backup_dir.path(), None).await?;
// import to context2
let backup = has_backup(&context2, backup_dir.path()).await?;
let context2_cloned = context2.clone();
let handle = task::spawn(async move {
imex(
&context2_cloned,
ImexMode::ImportBackup,
backup.as_ref(),
None,
)
.await
.unwrap();
});
while !handle.is_finished() {
// The database is still unconfigured;
// fill the config cache with the old value.
context2.is_configured().await.ok();
tokio::time::sleep(Duration::from_micros(1)).await;
}
// Assert that the config cache has the new value now.
assert!(context2.is_configured().await?);
Ok(())
}
#[test] #[test]
fn test_normalize_setup_code() { fn test_normalize_setup_code() {
let norm = normalize_setup_code("123422343234423452346234723482349234"); let norm = normalize_setup_code("123422343234423452346234723482349234");

View File

@@ -129,7 +129,8 @@ impl Sql {
.to_str() .to_str()
.with_context(|| format!("path {path:?} is not valid unicode"))? .with_context(|| format!("path {path:?} is not valid unicode"))?
.to_string(); .to_string();
self.call(move |conn| { let res = self
.call(move |conn| {
// Check that backup passphrase is correct before resetting our database. // Check that backup passphrase is correct before resetting our database.
conn.execute( conn.execute(
"ATTACH DATABASE ? AS backup KEY ?", "ATTACH DATABASE ? AS backup KEY ?",
@@ -165,7 +166,12 @@ impl Sql {
res?; res?;
Ok(()) Ok(())
}) })
.await .await;
// The config cache is wrong now that we have a different database
self.config_cache.write().await.clear();
res
} }
/// Creates a new connection pool. /// Creates a new connection pool.