From 070930e671aa25f286ead73e6c9e80e23b5fef49 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sun, 3 Mar 2024 14:01:02 +0200 Subject: [PATCH] meli/sqlite3: Fix auto index build when missing An error was returned from the db_path function, preventing the issuing of the reindex command in the background. Signed-off-by: Manos Pitsidianakis --- meli/src/accounts.rs | 2 +- meli/src/mail/status.rs | 13 +++++++++---- meli/src/sqlite3.rs | 36 ++++++++++++++++++------------------ 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/meli/src/accounts.rs b/meli/src/accounts.rs index f2b34c4f..9edcdc33 100644 --- a/meli/src/accounts.rs +++ b/meli/src/accounts.rs @@ -562,7 +562,7 @@ impl Account { ))); None } - Ok(path) => Some(path), + Ok(path) => path, }; if let Some(db_path) = db_path { if !db_path.exists() { diff --git a/meli/src/mail/status.rs b/meli/src/mail/status.rs index 68ecd01e..f0c0cd9e 100644 --- a/meli/src/mail/status.rs +++ b/meli/src/mail/status.rs @@ -19,6 +19,8 @@ * along with meli. If not, see . */ +use std::borrow::Cow; + use melib::{MailBackendExtensionStatus, SpecialUsageMailbox}; use super::*; @@ -171,16 +173,19 @@ impl AccountStatus { a.backend_capabilities.supports_search, ) { (SearchBackend::Auto, true) | (SearchBackend::None, true) => { - "backend-side search".to_string() + Cow::Borrowed("backend-side search") } (SearchBackend::Auto, false) | (SearchBackend::None, false) => { - "none (search will be slow)".to_string() + Cow::Borrowed("none (search will be slow)") } #[cfg(feature = "sqlite3")] (SearchBackend::Sqlite3, _) => { match crate::sqlite3::AccountCache::db_path(&a.name) { - Ok(path) => format!("sqlite3 database: {}", path.display()), - Err(err) => format!("sqlite3 error: {err}"), + Ok(Some(path)) => { + Cow::Owned(format!("sqlite3 database: {}", path.display())) + } + Ok(None) => Cow::Borrowed("sqlite3 database: uninitialized"), + Err(err) => Cow::Owned(format!("sqlite3 error: {err}")), } } }, diff --git a/meli/src/sqlite3.rs b/meli/src/sqlite3.rs index 842898db..a9f18f1c 100644 --- a/meli/src/sqlite3.rs +++ b/meli/src/sqlite3.rs @@ -186,7 +186,10 @@ impl AccountCache { "Failed to insert envelope {}: {err}", envelope.message_id_display(), ); - return Err(Error::new(err.to_string())); + return Err(Error::new(format!( + "Failed to insert envelope {}: {err}", + envelope.message_id_display(), + ))); } let account_id: i32 = { let mut stmt = tx @@ -245,30 +248,29 @@ impl AccountCache { pub async fn remove(acc_name: String, env_hash: EnvelopeHash) -> Result<()> { let db_desc = DatabaseDescription { - identifier: Some(acc_name.into()), + identifier: Some(acc_name.clone().into()), ..DB.clone() }; let db_path = db_desc.db_path()?; if !db_path.exists() { - return Err(Error::new( - "Database hasn't been initialised. Run `reindex {acc_name}` command", - )); + return Err(Error::new(format!( + "Database hasn't been initialised. Run `reindex {acc_name}` command" + ))); } smol::unblock(move || { let mut conn = db_desc.open_or_create_db()?; let tx = conn.transaction_with_behavior(melib::rusqlite::TransactionBehavior::Immediate)?; - if let Err(err) = tx - .execute( - "DELETE FROM envelopes WHERE hash = ?", - params![env_hash.to_be_bytes().to_vec(),], - ) - .map_err(|e| Error::new(e.to_string())) - { + if let Err(err) = tx.execute( + "DELETE FROM envelopes WHERE hash = ?", + params![env_hash.to_be_bytes().to_vec(),], + ) { drop(tx); log::error!("Failed to remove envelope {env_hash}: {err}"); - return Err(err); + return Err(Error::new(format!( + "Failed to remove envelope {env_hash}: {err}" + ))); } tx.commit()?; Ok(()) @@ -447,18 +449,16 @@ impl AccountCache { .await } - pub fn db_path(acc_name: &str) -> Result { + pub fn db_path(acc_name: &str) -> Result> { let db_desc = DatabaseDescription { identifier: Some(acc_name.to_string().into()), ..DB.clone() }; let db_path = db_desc.db_path()?; if !db_path.exists() { - return Err(Error::new( - "Database hasn't been initialised. Run `reindex {acc_name}` command", - )); + return Ok(None); } - Ok(db_path) + Ok(Some(db_path)) } }