Skip to content

Commit

Permalink
Storage: Migration 28 - fix empty petnames
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedilger committed Mar 12, 2024
1 parent c99f603 commit 7ba455d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
39 changes: 39 additions & 0 deletions gossip-lib/src/storage/migrations/m28.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::error::Error;
use crate::storage::types::Person2;
use crate::storage::Storage;
use heed::RwTxn;

impl Storage {
pub(super) fn m28_trigger(&self) -> Result<(), Error> {
let _ = self.db_people2()?;
Ok(())
}

pub(super) fn m28_migrate<'a>(
&'a self,
prefix: &str,
txn: &mut RwTxn<'a>,
) -> Result<(), Error> {
// Info message
tracing::info!("{prefix}: fixing empty petnames...");

// Migrate
self.m28_fix_empty_petnames(txn)?;

Ok(())
}

fn m28_fix_empty_petnames<'a>(&'a self, txn: &mut RwTxn<'a>) -> Result<(), Error> {
let loop_txn = self.env.read_txn()?;
for result in self.db_people2()?.iter(&loop_txn)? {
let (key, val) = result?;
let mut person: Person2 = serde_json::from_slice(val)?;
if person.petname == Some("".to_string()) {
person.petname = None;
let bytes = serde_json::to_vec(&person)?;
self.db_people2()?.put(txn, &key, &bytes)?;
}
}
Ok(())
}
}
5 changes: 4 additions & 1 deletion gossip-lib/src/storage/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod m24;
mod m25;
mod m26;
mod m27;
mod m28;
mod m3;
mod m4;
mod m5;
Expand All @@ -33,7 +34,7 @@ use crate::error::{Error, ErrorKind};
use heed::RwTxn;

impl Storage {
const MAX_MIGRATION_LEVEL: u32 = 27;
const MAX_MIGRATION_LEVEL: u32 = 28;

/// Initialize the database from empty
pub(super) fn init_from_empty(&self) -> Result<(), Error> {
Expand Down Expand Up @@ -115,6 +116,7 @@ impl Storage {
25 => self.m25_trigger()?,
26 => self.m26_trigger()?,
27 => self.m27_trigger()?,
28 => self.m28_trigger()?,
_ => panic!("Unreachable migration level"),
}

Expand Down Expand Up @@ -151,6 +153,7 @@ impl Storage {
25 => self.m25_migrate(&prefix, txn)?,
26 => self.m26_migrate(&prefix, txn)?,
27 => self.m27_migrate(&prefix, txn)?,
28 => self.m28_migrate(&prefix, txn)?,
_ => panic!("Unreachable migration level"),
};

Expand Down

0 comments on commit 7ba455d

Please sign in to comment.