This commit is contained in:
2025-02-27 17:51:07 +01:00
parent 3afd9cd8b2
commit abe52b2b66
3 changed files with 34 additions and 13 deletions

View File

@@ -56,6 +56,15 @@ impl Database {
self.read(|dbtx| dbtx.open_table(TABLE_VERSION)?.get(()).map(|o| o.map(|v| v.value())).map_err(Into::into))
.await
}
#[tracing::instrument(skip_all)]
pub async fn set_version(&self, version: Version) -> Result<(), DatabaseOperationError> {
self.write(|txn| {
let mut table = txn.open_table(TABLE_VERSION)?;
table.insert((), version)?;
Ok(())
}).await
}
}
#[derive(Debug, derive_more::Error, derive_more::Display, derive_more::From)]
@@ -75,7 +84,7 @@ impl DatabaseOperationError {
}
pub const TABLE_VERSION: TableDefinition<(), Bincode<Version>> = TableDefinition::new("version");
#[derive(Serialize, Deserialize, Debug, derive_more::Display)]
#[derive(Serialize, Deserialize, Debug, derive_more::Display, PartialEq, Eq, PartialOrd, Ord)]
#[display("{version}")]
pub struct Version {
pub version: u64,

View File

@@ -1,16 +1,20 @@
use super::db;
use super::db::Database;
use super::db::DatabaseOperationError;
use super::db::{self};
#[tracing::instrument(skip_all, err)]
pub async fn run_migrations(db: &Database) -> Result<(), Box<dyn std::error::Error>> {
v3(db).await?;
if is_at_version(db, 2).await? {
migrate_to_v3(db).await?;
}
Ok(())
}
#[tracing::instrument(skip_all, err)]
pub async fn v3(db: &Database) -> Result<(), Box<dyn std::error::Error>> {
pub async fn migrate_to_v3(db: &Database) -> Result<(), Box<dyn std::error::Error>> {
use redb::ReadableTableMetadata;
tracing::warn!("MIGRATING TO VERSION 3");
db.write(|txn| {
// Opening the table creates the table
let table = txn.open_table(db::current::TABLE_USER)?;
@@ -18,5 +22,13 @@ pub async fn v3(db: &Database) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
})
.await?;
db.set_version(db::Version {version: db::v3::VERSION}).await?;
Ok(())
}
async fn is_at_version(db: &Database, version: u64) -> Result<bool, DatabaseOperationError> {
let v = db.get_version().await?;
Ok(v == Some(db::Version { version }))
}