mirror of
https://github.com/mikedilger/chorus.git
synced 2026-08-01 07:21:39 +00:00
Store: migrations
This commit is contained in:
parent
4f41e4ad9a
commit
c302c553a2
46
src/store/migrations.rs
Normal file
46
src/store/migrations.rs
Normal file
@ -0,0 +1,46 @@
|
||||
use super::Store;
|
||||
use crate::error::Error;
|
||||
use heed::RwTxn;
|
||||
|
||||
pub const CURRENT_MIGRATION_LEVEL: u32 = 0;
|
||||
|
||||
impl Store {
|
||||
pub fn migrate(&self) -> Result<(), Error> {
|
||||
let mut txn = self.env.write_txn()?;
|
||||
|
||||
let mut migration_level = {
|
||||
let zero_bytes = 0_u32.to_be_bytes();
|
||||
let migration_level_bytes = self
|
||||
.general
|
||||
.get(&txn, b"migration_level")?
|
||||
.unwrap_or(zero_bytes.as_slice());
|
||||
u32::from_be_bytes(migration_level_bytes[..4].try_into().unwrap())
|
||||
};
|
||||
|
||||
log::info!("Storage migration level = {}", migration_level);
|
||||
|
||||
while migration_level < CURRENT_MIGRATION_LEVEL {
|
||||
self.migrate_to(&mut txn, migration_level + 1)?;
|
||||
migration_level += 1;
|
||||
self.general.put(
|
||||
&mut txn,
|
||||
b"migration_level",
|
||||
migration_level.to_be_bytes().as_slice(),
|
||||
)?;
|
||||
}
|
||||
|
||||
txn.commit()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(unreachable_code)]
|
||||
fn migrate_to(&self, _txn: &mut RwTxn<'_>, level: u32) -> Result<(), Error> {
|
||||
log::info!("Migrating database to {}", level);
|
||||
match level {
|
||||
_ => panic!("Unknown migration level {level}"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,8 @@
|
||||
pub mod event_store;
|
||||
pub use event_store::EventStore;
|
||||
|
||||
mod migrations;
|
||||
|
||||
use crate::error::{ChorusError, Error};
|
||||
use crate::types::{Event, Filter, Id, Kind, Pubkey, Time};
|
||||
use heed::byteorder::BigEndian;
|
||||
@ -94,7 +96,7 @@ impl Store {
|
||||
|
||||
let events = EventStore::new(event_map_file)?;
|
||||
|
||||
Ok(Store {
|
||||
let store = Store {
|
||||
general,
|
||||
events,
|
||||
env,
|
||||
@ -105,7 +107,12 @@ impl Store {
|
||||
deleted_offsets,
|
||||
deleted_events,
|
||||
allow_scraping,
|
||||
})
|
||||
};
|
||||
|
||||
// This is in migrations.rs
|
||||
store.migrate()?;
|
||||
|
||||
Ok(store)
|
||||
}
|
||||
|
||||
/// Sync the data to disk. This happens periodically, but sometimes it's useful to force
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user