diff --git a/util/src/migration/tests.rs b/util/src/migration/tests.rs index ee5ff574e..05229bee5 100644 --- a/util/src/migration/tests.rs +++ b/util/src/migration/tests.rs @@ -19,7 +19,7 @@ //! are performed in temp sub-directories. use common::*; -use migration::{Config, SimpleMigration, Manager}; +use migration::{Batch, Config, Error, SimpleMigration, Migration, Manager}; use kvdb::Database; use devtools::RandomTempPath; @@ -62,11 +62,10 @@ impl SimpleMigration for Migration0 { fn version(&self) -> u32 { 1 } - fn simple_migrate(&mut self, key: Vec, value: Vec) -> Option<(Vec, Vec)> { - let mut key = key; + fn simple_migrate(&mut self, mut key: Vec, mut value: Vec) -> Option<(Vec, Vec)> { key.push(0x11); - let mut value = value; value.push(0x22); + Some((key, value)) } } @@ -83,6 +82,31 @@ impl SimpleMigration for Migration1 { } } +struct AddsColumn; + +impl Migration for AddsColumn { + fn pre_columns(&self) -> Option { None } + + fn columns(&self) -> Option { Some(1) } + + fn version(&self) -> u32 { 1 } + + fn migrate(&mut self, source: &Database, config: &Config, dest: &mut Database, col: Option) -> Result<(), Error> { + let mut batch = Batch::new(config, col); + + for (key, value) in source.iter(col) { + try!(batch.insert(key.to_vec(), value.to_vec(), dest)); + } + + + if col == Some(1) { + try!(batch.insert(vec![1, 2, 3], vec![4, 5, 6], dest)); + } + + batch.commit(dest) + } +} + #[test] fn one_simple_migration() { let dir = RandomTempPath::create_dir(); @@ -189,3 +213,16 @@ fn is_migration_needed() { assert!(manager.is_needed(1)); assert!(!manager.is_needed(2)); } + +#[test] +fn pre_columns() { + let mut manager = Manager::new(Config::default()); + manager.add_migration(AddsColumn).unwrap(); + + let dir = RandomTempPath::create_dir(); + let db_path = db_path(dir.as_path()); + + // this shouldn't fail to open the database even though it's one column + // short of the one before it. + manager.execute(&db_path, 0).unwrap(); +}