From 5224357f8bce494bff7630837ad18c432bccdb30 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Fri, 30 Oct 2020 21:50:21 +0000 Subject: [PATCH] Add migration 2: remove old data. --- src/config.rs | 2 +- src/db/data_migrations.rs | 1 + src/db/variables/migrations.rs | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index c5e3a3a..cbd9a96 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 = 1; +const MIGRATION_VERSION: u32 = 2; #[derive(Error, Debug)] pub enum ConfigError { diff --git a/src/db/data_migrations.rs b/src/db/data_migrations.rs index 5f26446..ee46881 100644 --- a/src/db/data_migrations.rs +++ b/src/db/data_migrations.rs @@ -7,6 +7,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"), }; pub fn get_migrations(versions: &[u32]) -> Result, MigrationError> { diff --git a/src/db/variables/migrations.rs b/src/db/variables/migrations.rs index c116c35..0c32d24 100644 --- a/src/db/variables/migrations.rs +++ b/src/db/variables/migrations.rs @@ -92,3 +92,19 @@ pub(in crate::db) fn add_room_user_variable_count(db: &Database) -> Result<(), D tx_result?; //For some reason, it cannot infer the type Ok(()) } + +pub(in crate::db) fn delete_v0_schema(db: &Database) -> Result<(), DataError> { + let mut vars = db.variables.0.scan_prefix(""); + let mut batch = Batch::default(); + + while let Some(Ok((key, _))) = vars.next() { + let key = key.to_vec(); + + if !key.contains(&0xff) { + batch.remove(key); + } + } + + db.variables.0.apply_batch(batch)?; + Ok(()) +}