test case and handle in-place migration correctly

This commit is contained in:
Robert Habermeier 2017-02-26 19:22:51 +01:00
parent ed0a2567d8
commit ac82a838b8
2 changed files with 35 additions and 10 deletions

View File

@ -201,19 +201,23 @@ fn migrate_database(version: u32, db_path: PathBuf, mut migrations: MigrationMan
// migrate old database to the new one
let temp_path = migrations.execute(&db_path, version)?;
// create backup
fs::rename(&db_path, &backup_path)?;
// completely in-place migration leads to the paths being equal.
// in that case, no need to shuffle directories.
if temp_path != db_path {
// create backup
fs::rename(&db_path, &backup_path)?;
// replace the old database with the new one
if let Err(err) = fs::rename(&temp_path, &db_path) {
// if something went wrong, bring back backup
fs::rename(&backup_path, &db_path)?;
return Err(err.into());
// replace the old database with the new one
if let Err(err) = fs::rename(&temp_path, &db_path) {
// if something went wrong, bring back backup
fs::rename(&backup_path, &db_path)?;
return Err(err.into());
}
// remove backup
fs::remove_dir_all(&backup_path)?;
}
// remove backup
fs::remove_dir_all(&backup_path)?;
Ok(())
}

View File

@ -226,3 +226,24 @@ fn pre_columns() {
// short of the one before it.
manager.execute(&db_path, 0).unwrap();
}
#[test]
fn change_columns() {
use kvdb::DatabaseConfig;
let mut manager = Manager::new(Config::default());
manager.add_migration(::migration::ChangeColumns {
pre_columns: None,
post_columns: Some(4),
version: 1,
}).unwrap();
let dir = RandomTempPath::create_dir();
let db_path = db_path(dir.as_path());
let new_path = manager.execute(&db_path, 0).unwrap();
let config = DatabaseConfig::with_columns(Some(4));
let db = Database::open(&config, new_path.to_str().unwrap()).unwrap();
assert_eq!(db.num_columns(), 4);
}