From abe52b2b66ffeb254eb76526b7f9d68c238bb20d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asger=20Juul=20Brunsh=C3=B8j?= Date: Thu, 27 Feb 2025 17:51:07 +0100 Subject: [PATCH] wip: v3 --- crates/ascend/src/server/db.rs | 11 ++++++++++- crates/ascend/src/server/migrations.rs | 18 +++++++++++++++--- flake.lock | 18 +++++++++--------- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/crates/ascend/src/server/db.rs b/crates/ascend/src/server/db.rs index 3a607f7..bdd6854 100644 --- a/crates/ascend/src/server/db.rs +++ b/crates/ascend/src/server/db.rs @@ -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> = 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, diff --git a/crates/ascend/src/server/migrations.rs b/crates/ascend/src/server/migrations.rs index e8bb2d6..76ce98e 100644 --- a/crates/ascend/src/server/migrations.rs +++ b/crates/ascend/src/server/migrations.rs @@ -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> { - 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> { +pub async fn migrate_to_v3(db: &Database) -> Result<(), Box> { 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> { Ok(()) }) .await?; + +db.set_version(db::Version {version: db::v3::VERSION}).await?; + Ok(()) } + +async fn is_at_version(db: &Database, version: u64) -> Result { + let v = db.get_version().await?; + Ok(v == Some(db::Version { version })) +} diff --git a/flake.lock b/flake.lock index db2effe..c0ab54b 100644 --- a/flake.lock +++ b/flake.lock @@ -10,11 +10,11 @@ ] }, "locked": { - "lastModified": 1740139153, - "narHash": "sha256-Xa1wCQBbsFHCaXgVBjtraZcWywuXBN+YhdqGle4nLVc=", + "lastModified": 1740523129, + "narHash": "sha256-q/k/T9Hf+aCo8/xQnqyw+E7dYx8Nq1u7KQ2ylORcP+M=", "owner": "plul", "repo": "basecamp", - "rev": "0a29da733dc2f7b386dd3667b63a51c55238fbfd", + "rev": "0882906c106ab0bf193b3417c845c5accbec2419", "type": "github" }, "original": { @@ -25,11 +25,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1740396192, - "narHash": "sha256-ATMHHrg3sG1KgpQA5x8I+zcYpp5Sf17FaFj/fN+8OoQ=", + "lastModified": 1740547748, + "narHash": "sha256-Ly2fBL1LscV+KyCqPRufUBuiw+zmWrlJzpWOWbahplg=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "d9b69c3ec2a2e2e971c534065bdd53374bd68b97", + "rev": "3a05eebede89661660945da1f151959900903b6a", "type": "github" }, "original": { @@ -53,11 +53,11 @@ ] }, "locked": { - "lastModified": 1740450604, - "narHash": "sha256-T/lqASXzCzp5lJISCUw+qwfRmImVUnhKgAhn8ymRClI=", + "lastModified": 1740623427, + "narHash": "sha256-3SdPQrZoa4odlScFDUHd4CUPQ/R1gtH4Mq9u8CBiK8M=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "5961ca311c85c31fc5f51925b4356899eed36221", + "rev": "d342e8b5fd88421ff982f383c853f0fc78a847ab", "type": "github" }, "original": {