Skip to content

Commit 72862b5

Browse files
committed
Polish location check with vendor placeholder
Closes gh-10387
1 parent 1ee47ce commit 72862b5

File tree

2 files changed

+68
-39
lines changed

2 files changed

+68
-39
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.autoconfigure.flyway;
1818

1919
import java.util.Arrays;
20+
import java.util.Collection;
2021
import java.util.Collections;
2122
import java.util.HashSet;
2223
import java.util.List;
@@ -104,8 +105,6 @@ public static class FlywayConfiguration {
104105

105106
private List<FlywayCallback> flywayCallbacks;
106107

107-
private static final String VENDOR_PLACEHOLDER = "{vendor}";
108-
109108
public FlywayConfiguration(FlywayProperties properties,
110109
ResourceLoader resourceLoader, ObjectProvider<DataSource> dataSource,
111110
@FlywayDataSource ObjectProvider<DataSource> flywayDataSource,
@@ -136,49 +135,22 @@ else if (this.flywayDataSource != null) {
136135
}
137136
flyway.setCallbacks(this.flywayCallbacks
138137
.toArray(new FlywayCallback[this.flywayCallbacks.size()]));
139-
String[] locations = resolveLocations(this.properties.getLocations().toArray(new String[0]), flyway.getDataSource());
138+
String[] locations = new LocationResolver(flyway.getDataSource())
139+
.resolveLocations(this.properties.getLocations());
140140
checkLocationExists(locations);
141141
flyway.setLocations(locations);
142142
return flyway;
143143
}
144144

145-
private static String[] resolveLocations(String[] locations, DataSource dataSource) {
146-
if (usesVendorLocation(locations)) {
147-
try {
148-
String url = (String) JdbcUtils
149-
.extractDatabaseMetaData(dataSource, "getURL");
150-
DatabaseDriver vendor = DatabaseDriver.fromJdbcUrl(url);
151-
if (vendor != DatabaseDriver.UNKNOWN) {
152-
for (int i = 0; i < locations.length; i++) {
153-
locations[i] = locations[i].replace(VENDOR_PLACEHOLDER,
154-
vendor.getId());
155-
}
156-
}
157-
}
158-
catch (MetaDataAccessException ex) {
159-
throw new IllegalStateException(ex);
160-
}
161-
}
162-
return locations;
163-
}
164-
165-
private static boolean usesVendorLocation(String... locations) {
166-
for (String location : locations) {
167-
if (location.contains(VENDOR_PLACEHOLDER)) {
168-
return true;
169-
}
170-
}
171-
return false;
172-
}
173-
174145
private void checkLocationExists(String... locations) {
175146
if (this.properties.isCheckLocation()) {
176147
Assert.state(locations.length != 0,
177148
"Migration script locations not configured");
178149
boolean exists = hasAtLeastOneLocation(locations);
179150
Assert.state(exists,
180-
() -> "Cannot find migrations location in: " + Arrays.asList(locations)
181-
+ " (please add migrations or check your Flyway configuration)");
151+
() -> "Cannot find migrations location in: " + Arrays.asList(
152+
locations)
153+
+ " (please add migrations or check your Flyway configuration)");
182154
}
183155
}
184156

@@ -235,9 +207,64 @@ private static class SpringBootFlyway extends Flyway {
235207

236208
@Override
237209
public void setLocations(String... locations) {
238-
locations = FlywayConfiguration.resolveLocations(locations,
239-
getDataSource());
240-
super.setLocations(locations);
210+
super.setLocations(
211+
new LocationResolver(getDataSource()).resolveLocations(locations));
212+
}
213+
214+
}
215+
216+
private static class LocationResolver {
217+
218+
private static final String VENDOR_PLACEHOLDER = "{vendor}";
219+
220+
private final DataSource dataSource;
221+
222+
public LocationResolver(DataSource dataSource) {
223+
this.dataSource = dataSource;
224+
}
225+
226+
public String[] resolveLocations(Collection<String> locations) {
227+
return resolveLocations(locations.toArray(new String[locations.size()]));
228+
}
229+
230+
public String[] resolveLocations(String[] locations) {
231+
if (usesVendorLocation(locations)) {
232+
DatabaseDriver databaseDriver = getDatabaseDriver();
233+
return replaceVendorLocations(locations, databaseDriver);
234+
}
235+
return locations;
236+
}
237+
238+
private String[] replaceVendorLocations(String[] locations,
239+
DatabaseDriver databaseDriver) {
240+
if (databaseDriver == DatabaseDriver.UNKNOWN) {
241+
return locations;
242+
}
243+
String vendor = databaseDriver.getId();
244+
return Arrays.stream(locations)
245+
.map((location) -> location.replace(VENDOR_PLACEHOLDER, vendor))
246+
.toArray(String[]::new);
247+
}
248+
249+
private DatabaseDriver getDatabaseDriver() {
250+
try {
251+
String url = (String) JdbcUtils.extractDatabaseMetaData(this.dataSource,
252+
"getURL");
253+
return DatabaseDriver.fromJdbcUrl(url);
254+
}
255+
catch (MetaDataAccessException ex) {
256+
throw new IllegalStateException(ex);
257+
}
258+
259+
}
260+
261+
private boolean usesVendorLocation(String... locations) {
262+
for (String location : locations) {
263+
if (location.contains(VENDOR_PLACEHOLDER)) {
264+
return true;
265+
}
266+
}
267+
return false;
241268
}
242269

243270
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,13 @@ public void useVendorDirectory() {
258258
@Test
259259
public void useOneLocationWithVendorDirectory() {
260260
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
261-
.withPropertyValues("spring.flyway.locations=classpath:db/vendors/{vendor}")
261+
.withPropertyValues(
262+
"spring.flyway.locations=classpath:db/vendors/{vendor}")
262263
.run((context) -> {
263264
assertThat(context).hasSingleBean(Flyway.class);
264265
Flyway flyway = context.getBean(Flyway.class);
265-
assertThat(flyway.getLocations()).containsExactly("classpath:db/vendors/h2");
266+
assertThat(flyway.getLocations())
267+
.containsExactly("classpath:db/vendors/h2");
266268
});
267269
}
268270

0 commit comments

Comments
 (0)