Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
clean up migrations ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
keorn committed Jul 28, 2016
1 parent 3d38e05 commit 3ca319b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
11 changes: 4 additions & 7 deletions util/src/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,11 @@ impl Manager {

/// Adds new migration rules.
pub fn add_migration<T>(&mut self, migration: T) -> Result<(), Error> where T: Migration {
let version_match = match self.migrations.last() {
Some(last) => last.version() + 1 == migration.version(),
let is_new = match self.migrations.last() {
Some(last) => migration.version() > last.version(),
None => true,
};

match version_match {
match is_new {
true => Ok(self.migrations.push(Box::new(migration))),
false => Err(Error::CannotAddMigration),
}
Expand Down Expand Up @@ -238,9 +237,7 @@ impl Manager {

/// Find all needed migrations and arrange them.
fn migrations_from(&mut self, version: u32) -> Vec<&mut Box<Migration>> {
let mut to_apply: Vec<_> = self.migrations.iter_mut().filter(|m| m.version() > version).collect();
to_apply.sort_by_key(|m| m.version());
to_apply
self.migrations.iter_mut().filter(|m| m.version() > version).collect()
}
}

12 changes: 12 additions & 0 deletions util/src/migration/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ fn no_migration_needed() {
manager.execute(&db_path, 1).unwrap();
}

#[test]
#[should_panic]
fn wrong_adding_order() {
let dir = RandomTempPath::create_dir();
let db_path = db_path(dir.as_path());
let mut manager = Manager::new(Config::default());
make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]);

manager.add_migration(Migration1).unwrap();
manager.add_migration(Migration0).unwrap();
}

#[test]
fn multiple_migrations() {
let dir = RandomTempPath::create_dir();
Expand Down

0 comments on commit 3ca319b

Please sign in to comment.