MongoMigrationHistoryService implements MigrationHistoryService { @Override public Iterable<MigrationEntry> findMigrations(String fromVersion) { if (fromVersion == null) { return getMigramongoCollection().find().sort(new Document("createdAt", 1)).map(d -> mapMigrationEntry(d)); } final List<MigrationEntry> entriesFrom = new ArrayList<>(); String nextFromVersion = fromVersion; boolean initialFound = false; while (true) { final Bson fromVersionFilter = eq("fromVersion", nextFromVersion); final Bson upgradeFilter = eq("type", MigrationType.UPGRADE.name()); final FindIterable<Document> find = getMigramongoCollection().find(); if (initialFound) { find.filter(and(fromVersionFilter, upgradeFilter)); } else { find.filter(fromVersionFilter); } final MigrationEntry migEntry = find.map(d -> mapMigrationEntry(d)).first(); if (migEntry == null) { return entriesFrom; } entriesFrom.add(migEntry); nextFromVersion = migEntry.getToVersion(); if (migEntry.getMigrationType() == MigrationType.INITIAL) { initialFound = true; } } } MongoMigrationHistoryService(MongoDatabase database); @Override MigrationEntry insertMigrationStatusInProgress(MigrationInfo migrationInfo); @Override MigrationEntry insertMigrationStatusSkipped(MigrationInfo migrationInfo); @Override MigrationEntry setMigrationStatusToFailed(MigrationEntry migrationEntry, Exception e); @Override MigrationEntry setMigrationStatusToFinished(MigrationEntry migrationEntry); @Override MigrationEntry setMigrationStatusToManuallyRepaired(MigrationEntry migrationEntry); @Override MigrationEntry getLastMigrationApplied(); @Override Iterable<MigrationEntry> getAllMigrationsApplied(); @Override MigrationEntry findMigration(String fromVersion, String toVersion); @Override Iterable<MigrationEntry> findMigrations(String fromVersion); @Override MigrationEntry addRunToMigrationEntry(MigrationEntry migEntry, MigrationRun migRun); static final String MIGRAMONGO_COLLECTION; }
@Test public void shouldNotFindMigrationsIfNoneAvailable() throws Exception { final List<MigrationEntry> migrations = toList(migrationHistoryService.findMigrations("1")); assertThat(migrations, hasSize(0)); }
@Test public void shouldFindAllMigrationsIfFromIsNull() throws Exception { addMigration(new InitialMigrationInfo("1")); addMigration(new MigrationInfo("1", "2")); final List<MigrationEntry> migrations = toList(migrationHistoryService.findMigrations(null)); assertThat(migrations, hasSize(2)); }
@Test public void shouldFindMigrationsFromACorrectInitialVersion() throws Exception { addMigration(new InitialMigrationInfo("1")); addMigration(new MigrationInfo("1", "2")); final List<MigrationEntry> migrations = toList(migrationHistoryService.findMigrations("1")); assertThat(migrations, hasSize(2)); assertThat(migrations.get(0).getFromVersion(), is("1")); }
@Test public void shouldNotFindMigrationsIfFromDoesntExist() throws Exception { addMigration(new InitialMigrationInfo("1")); addMigration(new MigrationInfo("1", "2")); final List<MigrationEntry> migrations = toList(migrationHistoryService.findMigrations("42")); assertThat(migrations, hasSize(0)); }
@Test public void shouldFindMigrationsFromACorrectNonInitialVersion() throws Exception { addMigration(new InitialMigrationInfo("1")); addMigration(new MigrationInfo("1", "2")); addMigration(new MigrationInfo("2", "3")); addMigration(new MigrationInfo("3", "4")); addMigration(new MigrationInfo("4", "5")); final List<MigrationEntry> migrations = toList(migrationHistoryService.findMigrations("2")); assertThat(migrations, hasSize(3)); assertThat(migrations.get(0).getFromVersion(), is("2")); assertThat(migrations.get(2).getFromVersion(), is("4")); }