MigraMongo { public MigraMongoStatus rerun(String fromVersion, String toVersion) { final MigrationEntry migrationEntry = migrationHistoryService.findMigration(fromVersion, toVersion); if (migrationEntry == null) { return MigraMongoStatus .error( "No migration entry found for fromVersion '" + fromVersion + "' and toVersion '" + toVersion + "' in the migration history"); } final Optional<MongoMigrationScript> migScriptOpt = scriptLookupService .findMongoScripts() .stream() .filter( script -> fromVersion.equals(script.getMigrationInfo().getFromVersion()) && toVersion.equals(script.getMigrationInfo().getToVersion())) .findFirst(); if ( !migScriptOpt.isPresent()) { return MigraMongoStatus .error( "No migration script found for fromVersion '" + fromVersion + "' and toVersion '" + toVersion + "'"); } final MongoMigrationScript mongoMigrationScript = migScriptOpt.get(); final MigrationRun migrationRun = new MigrationRun(); try { mongoMigrationScript.migrate(database); final MigrationEntry migrationEntryWithRun = migrationHistoryService .addRunToMigrationEntry( migrationEntry, migrationRun.complete(MigrationStatus.OK, "Migration completed correctly")); return MigraMongoStatus .ok("Re-run of Migration fromVersion " + fromVersion + " toVersion " + toVersion + " run successfully") .addEntry(migrationEntryWithRun); } catch (Exception e) { logger .error( "Error when re-running migration fromVersion " + fromVersion + " toVersion " + toVersion + ": " + e.getMessage()); final MigrationEntry migrationEntryWithRun = migrationHistoryService .addRunToMigrationEntry(migrationEntry, migrationRun.complete(MigrationStatus.ERROR, e.getMessage())); return MigraMongoStatus .error( "Error when re-running migration fromVersion " + fromVersion + " toVersion " + toVersion + ": " + e.getMessage()) .addEntry(migrationEntryWithRun); } } MigraMongo(
            MongoDatabase database,
            MigrationHistoryService migrationEntryService,
            LockService lockService,
            ScriptLookupService scriptLookupService); void setAsyncInitializerFunction(Runnable function); MigraMongoStatus dryRun(); MigraMongoStatus migrate(); MigraMongoStatus migrateAsync(); MigraMongoStatus status(String fromVersion); MigraMongoStatus repair(String fromVersion, String toVersion); MigraMongoStatus rerun(String fromVersion, String toVersion); List<MigrationEntry> getMigrationEntries(); void destroyLocks();  }
@Test public void shouldNotRerunIfEntryDoesNotExist() throws Exception { final MigraMongoStatus status = migraMongo.rerun("NOTEXISTING1", "NOTEXISTING2"); assertThat(status.status, is(MigrationStatus.ERROR)); assertThat(status.message, containsString("No migration entry found")); assertThat(status.migrationsApplied, hasSize(0)); }
@Test public void shouldNotRerunIfScriptDoesNotExist() throws Exception { mockEntry("2", "3", MigrationStatus.OK); final List<MongoMigrationScript> mongoScripts = Arrays.asList(mockMongoScript("1", "2", (Boolean) null)); when(lookupService.findMongoScripts()).thenReturn(mongoScripts); final MigraMongoStatus status = migraMongo.rerun("2", "3"); assertThat(status.status, is(MigrationStatus.ERROR)); assertThat(status.message, containsString("No migration script found")); assertThat(status.migrationsApplied, hasSize(0)); }