From 9f598541fb27e5124a216a895b1d1ed496e9fbd3 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Fri, 30 Oct 2020 22:44:19 +0000 Subject: [PATCH] Remove variable_count keys missed in manual data migration. --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/config.rs | 2 +- src/db/data_migrations.rs | 1 + src/db/variables/migrations.rs | 24 ++++++++++++++++++++++++ 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 8e5e42b..60bbe76 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -197,6 +197,7 @@ dependencies = [ "itertools", "log", "matrix-sdk", + "memmem", "nom", "olm-sys", "phf", @@ -1043,6 +1044,12 @@ version = "2.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" +[[package]] +name = "memmem" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a64a92489e2744ce060c349162be1c5f33c6969234104dbd99ddb5feb08b8c15" + [[package]] name = "memoffset" version = "0.5.6" diff --git a/Cargo.toml b/Cargo.toml index 125bef3..32b6968 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ sled = "0.34" zerocopy = "0.3" byteorder = "1.3" futures = "0.3" +memmem = "0.1" phf = { version = "0.7", features = ["macros"] } olm-sys = "1.0" matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "master" } diff --git a/src/config.rs b/src/config.rs index cbd9a96..cddfe10 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,7 +6,7 @@ use thiserror::Error; /// Shortcut to defining db migration versions. Will probably /// eventually be moved to a config file. -const MIGRATION_VERSION: u32 = 2; +const MIGRATION_VERSION: u32 = 3; #[derive(Error, Debug)] pub enum ConfigError { diff --git a/src/db/data_migrations.rs b/src/db/data_migrations.rs index ee46881..e44aa3b 100644 --- a/src/db/data_migrations.rs +++ b/src/db/data_migrations.rs @@ -8,6 +8,7 @@ pub(super) type DataMigration = (fn(&Database) -> Result<(), DataError>, &'stati static MIGRATIONS: phf::Map = phf_map! { 1u32 => (add_room_user_variable_count, "add_room_user_variable_count"), 2u32 => (delete_v0_schema, "delete_v0_schema"), + 3u32 => (delete_variable_count, "delete_variable_count"), }; pub fn get_migrations(versions: &[u32]) -> Result, MigrationError> { diff --git a/src/db/variables/migrations.rs b/src/db/variables/migrations.rs index 0c32d24..0bff9dd 100644 --- a/src/db/variables/migrations.rs +++ b/src/db/variables/migrations.rs @@ -2,6 +2,7 @@ use super::*; use crate::db::errors::{DataError, MigrationError}; use crate::db::Database; use byteorder::LittleEndian; +use memmem::{Searcher, TwoWaySearcher}; use sled::transaction::TransactionError; use sled::{Batch, IVec}; use zerocopy::byteorder::U32; @@ -108,3 +109,26 @@ pub(in crate::db) fn delete_v0_schema(db: &Database) -> Result<(), DataError> { db.variables.0.apply_batch(batch)?; Ok(()) } + +pub(in crate::db) fn delete_variable_count(db: &Database) -> Result<(), DataError> { + let prefix = variables_space_prefix(""); + let mut vars = db.variables.0.scan_prefix(prefix); + let mut batch = Batch::default(); + + while let Some(Ok((key, _))) = vars.next() { + let search = TwoWaySearcher::new(b"variable_count"); + let ends_with = { + match search.search_in(&key) { + Some(index) => key.len() - index == b"variable_count".len(), + None => false, + } + }; + + if ends_with { + batch.remove(key); + } + } + + db.variables.0.apply_batch(batch)?; + Ok(()) +}