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 }))
}

18
flake.lock generated
View File

@@ -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": {