MigrationManager { public boolean migrate(Version from, Version to) { logger.info("Migrating from "+from+" to "+to); AtomicBoolean successful = new AtomicBoolean(true); List<MigrationTask> matchedMigrators = new ArrayList<>(); boolean upgrade = to.compareTo(from)>=1; for(MigrationTask migrator:migrators) { if(migrator.asOfVersion.compareTo(upgrade?from:to)>=1 && migrator.asOfVersion.compareTo(upgrade?to:from)<=0) { matchedMigrators.add(migrator); } } matchedMigrators.sort(new Comparator<MigrationTask>() { @Override public int compare(MigrationTask o1, MigrationTask o2) { return (upgrade?1:-1)*o1.asOfVersion.compareTo(o2.asOfVersion); } }); matchedMigrators.forEach(m->{ logger.info("Running migration task "+m); long t1 = System.currentTimeMillis(); try { if(upgrade) { m.runUpgradeScript(); } else { m.runDowngradeScript(); } logger.info("Migration task "+m+" successfully executed in "+(System.currentTimeMillis()-t1)+"ms"); } catch(Exception e) { logger.error("Error while running upgrade/downgrade script "+m, e); successful.set(false); } }); return successful.get(); } MigrationManager(GlobalContext context); void register(MigrationTask migrationTask); boolean migrate(Version from, Version to);  }
@Test public void testUpgrade() { StringBuilder s = new StringBuilder(); MigrationManager m = getMigrationManager(s); m.migrate(new Version(1, 2, 2), new Version(1, 2, 4)); assertEquals("1.2.3,1.2.4,", s.toString()); }
@Test public void testUpgrade2() { StringBuilder s = new StringBuilder(); MigrationManager m = getMigrationManager(s); m.migrate(new Version(1, 2, 2), new Version(5, 2, 4)); assertEquals("1.2.3,1.2.4,1.2.5,2.2.5,", s.toString()); }
@Test public void testDowngrade() { StringBuilder s = new StringBuilder(); MigrationManager m = getMigrationManager(s); m.migrate(new Version(1, 2, 4), new Version(1, 2, 2)); assertEquals("-1.2.4,-1.2.3,", s.toString()); }