refactor: Reduce boilerplate for migration version increment

This commit is contained in:
iequidoo
2024-07-15 11:20:33 -03:00
committed by iequidoo
parent 9996c2db80
commit 5143ebece1
2 changed files with 16 additions and 5 deletions

View File

@@ -11,6 +11,7 @@ use crate::imap;
use crate::message::MsgId;
use crate::provider::get_provider_by_domain;
use crate::sql::Sql;
use crate::tools::inc_and_check;
const DBVERSION: i32 = 68;
const VERSION_CFG: &str = "dbversion";
@@ -941,10 +942,9 @@ CREATE INDEX msgs_status_updates_index2 ON msgs_status_updates (uid);
sql.execute_migration("ALTER TABLE msgs ADD COLUMN txt_normalized TEXT", 115)
.await?;
}
let migration_version: i32 = 115;
let mut migration_version: i32 = 115;
let migration_version: i32 = migration_version + 1;
ensure!(migration_version == 116, "Fix the number here");
inc_and_check(&mut migration_version, 116)?;
if dbversion < migration_version {
// Whether the message part doesn't need to be stored on the server. If all parts are marked
// deleted, a server-side deletion is issued.

View File

@@ -6,7 +6,7 @@
use std::borrow::Cow;
use std::io::{Cursor, Write};
use std::mem;
use std::ops::Deref;
use std::ops::{AddAssign, Deref};
use std::path::{Path, PathBuf};
use std::str::from_utf8;
// If a time value doesn't need to be sent to another host, saved to the db or otherwise used across
@@ -20,7 +20,7 @@ pub use std::time::SystemTime as Time;
#[cfg(not(test))]
pub use std::time::SystemTime;
use anyhow::{bail, Context as _, Result};
use anyhow::{bail, ensure, Context as _, Result};
use base64::Engine as _;
use chrono::{Local, NaiveDateTime, NaiveTime, TimeZone};
use deltachat_contact_tools::EmailAddress;
@@ -30,6 +30,7 @@ use futures::{StreamExt, TryStreamExt};
use mailparse::dateparse;
use mailparse::headers::Headers;
use mailparse::MailHeaderMap;
use num_traits::PrimInt;
use rand::{thread_rng, Rng};
use tokio::{fs, io};
use url::Url;
@@ -683,6 +684,16 @@ pub(crate) fn buf_decompress(buf: &[u8]) -> Result<Vec<u8>> {
Ok(mem::take(decompressor.get_mut()))
}
/// Increments `*t` and checks that it equals to `expected` after that.
pub(crate) fn inc_and_check<T: PrimInt + AddAssign + std::fmt::Debug>(
t: &mut T,
expected: T,
) -> Result<()> {
*t += T::one();
ensure!(*t == expected, "Incremented value != {expected:?}");
Ok(())
}
#[cfg(test)]
mod tests {
#![allow(clippy::indexing_slicing)]