melib/sqlite3: reset db on version mismatch

jmap-eventsource
Manos Pitsidianakis 2020-10-18 17:41:06 +03:00
parent 54cb4ea623
commit b9f4d718c7
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
2 changed files with 57 additions and 42 deletions

View File

@ -321,7 +321,7 @@ impl MailBackend for ImapType {
None
};
let mut state = FetchState {
stage: if self.uid_store.keep_offline_cache {
stage: if self.uid_store.keep_offline_cache && cache_handle.is_some() {
FetchStage::InitialCache
} else {
FetchStage::InitialFresh

View File

@ -50,6 +50,8 @@ pub fn open_or_create_db(
description: &DatabaseDescription,
identifier: Option<&str>,
) -> Result<Connection> {
let mut second_try: bool = false;
loop {
let db_path = if let Some(id) = identifier {
db_path(&format!("{}_{}", id, description.name))
} else {
@ -79,11 +81,23 @@ pub fn open_or_create_db(
}
let version: i32 = conn.pragma_query_value(None, "user_version", |row| row.get(0))?;
if version != 0_i32 && version as u32 != description.version {
return Err(MeliError::new(format!(
log(
format!(
"Database version mismatch, is {} but expected {}",
version, description.version
),
crate::INFO,
);
if second_try {
return Err(MeliError::new(format!(
"Database version mismatch, is {} but expected {}. Could not recreate database.",
version, description.version
)));
}
reset_db(description, identifier)?;
second_try = true;
continue;
}
if version == 0 {
conn.pragma_update(None, "user_version", &description.version)?;
@ -93,7 +107,8 @@ pub fn open_or_create_db(
.map_err(|e| MeliError::new(e.to_string()))?;
}
Ok(conn)
return Ok(conn);
}
}
/// Return database to a clean slate.