Skip to content

Commit 6235ef2

Browse files
committed
fix incorrect order of the application of additional flyway migrations in some rare cases
1 parent b899655 commit 6235ef2

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

embedded-database-spring-test-core/src/main/java/io/zonky/test/db/flyway/OptimizedFlywayTestExecutionListener.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private static boolean isAppendable(Flyway flyway, FlywayTest annotation) {
154154
return true;
155155
}
156156

157-
MigrationVersion testVersion = findLastVersion(flyway, annotation.locationsForMigrate());
157+
MigrationVersion testVersion = findFirstVersion(flyway, annotation.locationsForMigrate());
158158
if (testVersion == MigrationVersion.EMPTY) {
159159
return true;
160160
}
@@ -163,6 +163,17 @@ private static boolean isAppendable(Flyway flyway, FlywayTest annotation) {
163163
return coreVersion.compareTo(testVersion) < 0;
164164
}
165165

166+
private static MigrationVersion findFirstVersion(Flyway flyway, String... locations) {
167+
CompositeMigrationResolver resolver = createMigrationResolver(flyway, locations);
168+
List<ResolvedMigration> migrations = resolver.resolveMigrations();
169+
170+
if (CollectionUtils.isEmpty(migrations)) {
171+
return MigrationVersion.EMPTY;
172+
} else {
173+
return migrations.get(0).getVersion();
174+
}
175+
}
176+
166177
private static MigrationVersion findLastVersion(Flyway flyway, String... locations) {
167178
CompositeMigrationResolver resolver = createMigrationResolver(flyway, locations);
168179
List<ResolvedMigration> migrations = resolver.resolveMigrations();

embedded-database-spring-test-core/src/test/java/io/zonky/test/db/postgres/FlywayMigrationIntegrationTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,11 @@ public void loadDependentTestMigrations() throws Exception {
100100
assertThat(dataSource).isNotNull();
101101

102102
List<Map<String, Object>> persons = jdbcTemplate.queryForList(SQL_SELECT_PERSONS);
103-
assertThat(persons).isNotNull().hasSize(1);
103+
assertThat(persons).isNotNull().hasSize(2);
104104

105-
Map<String, Object> person = persons.get(0);
106-
assertThat(person).containsExactly(
107-
entry("id", 1L),
108-
entry("first_name", "Dave"),
109-
entry("last_name", "Syer"),
110-
entry("full_name", "Dave Syer"));
105+
assertThat(persons).extracting("id", "first_name", "last_name", "full_name").containsExactlyInAnyOrder(
106+
tuple(1L, "Dave", "Syer", "Dave Syer"),
107+
tuple(3L, "Will", "Smith", "Will Smith"));
111108
}
112109

113110
@Test

embedded-database-spring-test-core/src/test/java/io/zonky/test/db/postgres/MultipleFlywayBeansIntegrationTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,11 @@ public void databaseShouldBeLoadedByFlyway1() throws Exception {
9292
assertThat(dataSource).isNotNull();
9393

9494
List<Map<String, Object>> persons = jdbcTemplate.queryForList(SQL_SELECT_PERSONS);
95-
assertThat(persons).isNotNull().hasSize(1);
95+
assertThat(persons).isNotNull().hasSize(2);
9696

97-
Map<String, Object> person = persons.get(0);
98-
assertThat(person).containsExactly(
99-
entry("id", 1L),
100-
entry("first_name", "Dave"),
101-
entry("last_name", "Syer"),
102-
entry("full_name", "Dave Syer"));
97+
assertThat(persons).extracting("id", "first_name", "last_name", "full_name").containsExactlyInAnyOrder(
98+
tuple(1L, "Dave", "Syer", "Dave Syer"),
99+
tuple(3L, "Will", "Smith", "Will Smith"));
103100
}
104101

105102
@Test
@@ -140,10 +137,11 @@ public void databaseShouldBeLoadedByFlyway1AndAppendedByFlyway3() throws Excepti
140137
assertThat(dataSource).isNotNull();
141138

142139
List<Map<String, Object>> persons = jdbcTemplate.queryForList(SQL_SELECT_PERSONS);
143-
assertThat(persons).isNotNull().hasSize(2);
140+
assertThat(persons).isNotNull().hasSize(3);
144141

145142
assertThat(persons).extracting("id", "first_name", "last_name").containsExactlyInAnyOrder(
146143
tuple(1L, "Dave", "Syer"),
147-
tuple(2L, "Tom", "Hanks"));
144+
tuple(2L, "Tom", "Hanks"),
145+
tuple(3L, "Will", "Smith"));
148146
}
149147
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
insert into test.person (id, first_name, last_name, full_name) values (3, 'Will', 'Smith', 'Will Smith');

0 commit comments

Comments
 (0)