From 7f5b59afbbc38fd6fb294f068946ef289234c0b9 Mon Sep 17 00:00:00 2001 From: strawberry Date: Sat, 8 Jun 2024 16:58:24 -0400 Subject: [PATCH] add conduwuit-specific db migration fixing double split db entries for a few months now we accidentally had double 0xFF splits being inserted into `roomuserid_joined` cf when membership counts and such are being updated. this is a conduwuit-specific db migration and does NOT break conduit compatibility. `fix_bad_double_separator_in_state_cache` Signed-off-by: strawberry --- src/service/globals/migrations.rs | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/service/globals/migrations.rs b/src/service/globals/migrations.rs index 75990df0..3127c3ec 100644 --- a/src/service/globals/migrations.rs +++ b/src/service/globals/migrations.rs @@ -6,6 +6,7 @@ use std::{ sync::Arc, }; +use conduit::{debug_info, debug_warn}; use database::KeyValueDatabase; use itertools::Itertools; use ruma::{ @@ -547,6 +548,37 @@ pub(crate) async fn migrations(db: &KeyValueDatabase, config: &Config) -> Result } } + if db + .global + .get(b"fix_bad_double_separator_in_state_cache")? + .is_none() + { + warn!("Fixing bad double separator in state_cache roomuserid_joined"); + let mut iter_count: usize = 0; + for (mut key, value) in db.roomuserid_joined.iter() { + iter_count = iter_count.saturating_add(1); + debug_info!(%iter_count); + let first_sep_index = key.iter().position(|&i| i == 0xFF).unwrap(); + + if key + .iter() + .get(first_sep_index..=first_sep_index + 1) + .copied() + .collect_vec() == vec![0xFF, 0xFF] + { + debug_warn!("Found bad key: {key:?}"); + db.roomuserid_joined.remove(&key)?; + + key.remove(first_sep_index); + debug_warn!("Fixed key: {key:?}"); + db.roomuserid_joined.insert(&key, &value)?; + } + } + + db.global + .insert(b"fix_bad_double_separator_in_state_cache", &[])?; + } + assert_eq!( services().globals.database_version().unwrap(), latest_database_version, @@ -614,6 +646,9 @@ pub(crate) async fn migrations(db: &KeyValueDatabase, config: &Config) -> Result .globals .bump_database_version(latest_database_version)?; + db.global + .insert(b"fix_bad_double_separator_in_state_cache", &[])?; + // Create the admin room and server user on first run services().admin.create_admin_room().await?;