Skip to content

Commit

Permalink
Migration 31 - invert public/private sense in person lists
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedilger committed Apr 22, 2024
1 parent f532d63 commit 0aa350e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
46 changes: 46 additions & 0 deletions gossip-lib/src/storage/migrations/m31.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::error::Error;
use crate::misc::Private;
use crate::storage::types::PersonList1;
use crate::storage::Storage;
use heed::RwTxn;
use nostr_types::PublicKey;
use speedy::Readable;
use std::collections::HashMap;

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

pub(super) fn m31_migrate<'a>(
&'a self,
prefix: &str,
txn: &mut RwTxn<'a>,
) -> Result<(), Error> {
// Info message
tracing::info!("{prefix}: Changing sense of person lists public/private flag...");

// Migrate
self.m31_change_sense_of_person_lists_private(txn)?;

Ok(())
}

fn m31_change_sense_of_person_lists_private<'a>(
&'a self,
txn: &mut RwTxn<'a>,
) -> Result<(), Error> {
let loop_txn = self.env.read_txn()?;
for result in self.db_person_lists2()?.iter(&loop_txn)? {
let (key, val) = result?;
let pubkey = PublicKey::from_bytes(key, true)?;
let mut map = HashMap::<PersonList1, bool>::read_from_buffer(val)?;
let map2: HashMap<PersonList1, Private> =
map.drain().map(|(k, v)| (k, Private(!v))).collect();
self.write_person_lists2(&pubkey, map2, Some(txn))?;
}

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 @@ -24,6 +24,7 @@ mod m28;
mod m29;
mod m3;
mod m30;
mod m31;
mod m4;
mod m5;
mod m6;
Expand All @@ -36,7 +37,7 @@ use crate::error::{Error, ErrorKind};
use heed::RwTxn;

impl Storage {
const MAX_MIGRATION_LEVEL: u32 = 30;
const MAX_MIGRATION_LEVEL: u32 = 31;

/// Initialize the database from empty
pub(super) fn init_from_empty(&self) -> Result<(), Error> {
Expand Down Expand Up @@ -121,6 +122,7 @@ impl Storage {
28 => self.m28_trigger()?,
29 => self.m29_trigger()?,
30 => self.m30_trigger()?,
31 => self.m31_trigger()?,
_ => panic!("Unreachable migration level"),
}

Expand Down Expand Up @@ -160,6 +162,7 @@ impl Storage {
28 => self.m28_migrate(&prefix, txn)?,
29 => self.m29_migrate(&prefix, txn)?,
30 => self.m30_migrate(&prefix, txn)?,
31 => self.m31_migrate(&prefix, txn)?,
_ => panic!("Unreachable migration level"),
};

Expand Down

0 comments on commit 0aa350e

Please sign in to comment.