diff --git a/parity/migration.rs b/parity/migration.rs index 797e8d2d6..0d99bf250 100644 --- a/parity/migration.rs +++ b/parity/migration.rs @@ -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(()) } diff --git a/util/src/migration/tests.rs b/util/src/migration/tests.rs index 852b96a8d..ad80ddbc3 100644 --- a/util/src/migration/tests.rs +++ b/util/src/migration/tests.rs @@ -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); +}