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 <manos@pitsidianak.is>
pull/359/head
Manos Pitsidianakis 3 months ago
parent c7aee72525
commit 070930e671
No known key found for this signature in database
GPG Key ID: 7729C7707F7E09D0

@ -562,7 +562,7 @@ impl Account {
))); )));
None None
} }
Ok(path) => Some(path), Ok(path) => path,
}; };
if let Some(db_path) = db_path { if let Some(db_path) = db_path {
if !db_path.exists() { if !db_path.exists() {

@ -19,6 +19,8 @@
* along with meli. If not, see <http://www.gnu.org/licenses/>. * along with meli. If not, see <http://www.gnu.org/licenses/>.
*/ */
use std::borrow::Cow;
use melib::{MailBackendExtensionStatus, SpecialUsageMailbox}; use melib::{MailBackendExtensionStatus, SpecialUsageMailbox};
use super::*; use super::*;
@ -171,16 +173,19 @@ impl AccountStatus {
a.backend_capabilities.supports_search, a.backend_capabilities.supports_search,
) { ) {
(SearchBackend::Auto, true) | (SearchBackend::None, true) => { (SearchBackend::Auto, true) | (SearchBackend::None, true) => {
"backend-side search".to_string() Cow::Borrowed("backend-side search")
} }
(SearchBackend::Auto, false) | (SearchBackend::None, false) => { (SearchBackend::Auto, false) | (SearchBackend::None, false) => {
"none (search will be slow)".to_string() Cow::Borrowed("none (search will be slow)")
} }
#[cfg(feature = "sqlite3")] #[cfg(feature = "sqlite3")]
(SearchBackend::Sqlite3, _) => { (SearchBackend::Sqlite3, _) => {
match crate::sqlite3::AccountCache::db_path(&a.name) { match crate::sqlite3::AccountCache::db_path(&a.name) {
Ok(path) => format!("sqlite3 database: {}", path.display()), Ok(Some(path)) => {
Err(err) => format!("sqlite3 error: {err}"), Cow::Owned(format!("sqlite3 database: {}", path.display()))
}
Ok(None) => Cow::Borrowed("sqlite3 database: uninitialized"),
Err(err) => Cow::Owned(format!("sqlite3 error: {err}")),
} }
} }
}, },

@ -186,7 +186,10 @@ impl AccountCache {
"Failed to insert envelope {}: {err}", "Failed to insert envelope {}: {err}",
envelope.message_id_display(), 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 account_id: i32 = {
let mut stmt = tx let mut stmt = tx
@ -245,30 +248,29 @@ impl AccountCache {
pub async fn remove(acc_name: String, env_hash: EnvelopeHash) -> Result<()> { pub async fn remove(acc_name: String, env_hash: EnvelopeHash) -> Result<()> {
let db_desc = DatabaseDescription { let db_desc = DatabaseDescription {
identifier: Some(acc_name.into()), identifier: Some(acc_name.clone().into()),
..DB.clone() ..DB.clone()
}; };
let db_path = db_desc.db_path()?; let db_path = db_desc.db_path()?;
if !db_path.exists() { if !db_path.exists() {
return Err(Error::new( return Err(Error::new(format!(
"Database hasn't been initialised. Run `reindex {acc_name}` command", "Database hasn't been initialised. Run `reindex {acc_name}` command"
)); )));
} }
smol::unblock(move || { smol::unblock(move || {
let mut conn = db_desc.open_or_create_db()?; let mut conn = db_desc.open_or_create_db()?;
let tx = let tx =
conn.transaction_with_behavior(melib::rusqlite::TransactionBehavior::Immediate)?; conn.transaction_with_behavior(melib::rusqlite::TransactionBehavior::Immediate)?;
if let Err(err) = tx if let Err(err) = tx.execute(
.execute( "DELETE FROM envelopes WHERE hash = ?",
"DELETE FROM envelopes WHERE hash = ?", params![env_hash.to_be_bytes().to_vec(),],
params![env_hash.to_be_bytes().to_vec(),], ) {
)
.map_err(|e| Error::new(e.to_string()))
{
drop(tx); drop(tx);
log::error!("Failed to remove envelope {env_hash}: {err}"); 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()?; tx.commit()?;
Ok(()) Ok(())
@ -447,18 +449,16 @@ impl AccountCache {
.await .await
} }
pub fn db_path(acc_name: &str) -> Result<PathBuf> { pub fn db_path(acc_name: &str) -> Result<Option<PathBuf>> {
let db_desc = DatabaseDescription { let db_desc = DatabaseDescription {
identifier: Some(acc_name.to_string().into()), identifier: Some(acc_name.to_string().into()),
..DB.clone() ..DB.clone()
}; };
let db_path = db_desc.db_path()?; let db_path = db_desc.db_path()?;
if !db_path.exists() { if !db_path.exists() {
return Err(Error::new( return Ok(None);
"Database hasn't been initialised. Run `reindex {acc_name}` command",
));
} }
Ok(db_path) Ok(Some(db_path))
} }
} }

Loading…
Cancel
Save