Add migration 2: remove old data.

This commit is contained in:
projectmoon 2020-10-30 21:50:21 +00:00 committed by ProjectMoon
parent 6b21b0aff8
commit 5224357f8b
3 changed files with 18 additions and 1 deletions

View File

@ -6,7 +6,7 @@ use thiserror::Error;
/// Shortcut to defining db migration versions. Will probably /// Shortcut to defining db migration versions. Will probably
/// eventually be moved to a config file. /// eventually be moved to a config file.
const MIGRATION_VERSION: u32 = 1; const MIGRATION_VERSION: u32 = 2;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum ConfigError { pub enum ConfigError {

View File

@ -7,6 +7,7 @@ pub(super) type DataMigration = (fn(&Database) -> Result<(), DataError>, &'stati
static MIGRATIONS: phf::Map<u32, DataMigration> = phf_map! { static MIGRATIONS: phf::Map<u32, DataMigration> = phf_map! {
1u32 => (add_room_user_variable_count, "add_room_user_variable_count"), 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<Vec<DataMigration>, MigrationError> { pub fn get_migrations(versions: &[u32]) -> Result<Vec<DataMigration>, MigrationError> {

View File

@ -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 tx_result?; //For some reason, it cannot infer the type
Ok(()) 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(())
}