Commit 72862b56 authored by Phillip Webb's avatar Phillip Webb

Polish location check with vendor placeholder

Closes gh-10387
parent 1ee47cec
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.flyway; package org.springframework.boot.autoconfigure.flyway;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
...@@ -104,8 +105,6 @@ public class FlywayAutoConfiguration { ...@@ -104,8 +105,6 @@ public class FlywayAutoConfiguration {
private List<FlywayCallback> flywayCallbacks; private List<FlywayCallback> flywayCallbacks;
private static final String VENDOR_PLACEHOLDER = "{vendor}";
public FlywayConfiguration(FlywayProperties properties, public FlywayConfiguration(FlywayProperties properties,
ResourceLoader resourceLoader, ObjectProvider<DataSource> dataSource, ResourceLoader resourceLoader, ObjectProvider<DataSource> dataSource,
@FlywayDataSource ObjectProvider<DataSource> flywayDataSource, @FlywayDataSource ObjectProvider<DataSource> flywayDataSource,
...@@ -136,49 +135,22 @@ public class FlywayAutoConfiguration { ...@@ -136,49 +135,22 @@ public class FlywayAutoConfiguration {
} }
flyway.setCallbacks(this.flywayCallbacks flyway.setCallbacks(this.flywayCallbacks
.toArray(new FlywayCallback[this.flywayCallbacks.size()])); .toArray(new FlywayCallback[this.flywayCallbacks.size()]));
String[] locations = resolveLocations(this.properties.getLocations().toArray(new String[0]), flyway.getDataSource()); String[] locations = new LocationResolver(flyway.getDataSource())
.resolveLocations(this.properties.getLocations());
checkLocationExists(locations); checkLocationExists(locations);
flyway.setLocations(locations); flyway.setLocations(locations);
return flyway; return flyway;
} }
private static String[] resolveLocations(String[] locations, DataSource dataSource) {
if (usesVendorLocation(locations)) {
try {
String url = (String) JdbcUtils
.extractDatabaseMetaData(dataSource, "getURL");
DatabaseDriver vendor = DatabaseDriver.fromJdbcUrl(url);
if (vendor != DatabaseDriver.UNKNOWN) {
for (int i = 0; i < locations.length; i++) {
locations[i] = locations[i].replace(VENDOR_PLACEHOLDER,
vendor.getId());
}
}
}
catch (MetaDataAccessException ex) {
throw new IllegalStateException(ex);
}
}
return locations;
}
private static boolean usesVendorLocation(String... locations) {
for (String location : locations) {
if (location.contains(VENDOR_PLACEHOLDER)) {
return true;
}
}
return false;
}
private void checkLocationExists(String... locations) { private void checkLocationExists(String... locations) {
if (this.properties.isCheckLocation()) { if (this.properties.isCheckLocation()) {
Assert.state(locations.length != 0, Assert.state(locations.length != 0,
"Migration script locations not configured"); "Migration script locations not configured");
boolean exists = hasAtLeastOneLocation(locations); boolean exists = hasAtLeastOneLocation(locations);
Assert.state(exists, Assert.state(exists,
() -> "Cannot find migrations location in: " + Arrays.asList(locations) () -> "Cannot find migrations location in: " + Arrays.asList(
+ " (please add migrations or check your Flyway configuration)"); locations)
+ " (please add migrations or check your Flyway configuration)");
} }
} }
...@@ -235,9 +207,64 @@ public class FlywayAutoConfiguration { ...@@ -235,9 +207,64 @@ public class FlywayAutoConfiguration {
@Override @Override
public void setLocations(String... locations) { public void setLocations(String... locations) {
locations = FlywayConfiguration.resolveLocations(locations, super.setLocations(
getDataSource()); new LocationResolver(getDataSource()).resolveLocations(locations));
super.setLocations(locations); }
}
private static class LocationResolver {
private static final String VENDOR_PLACEHOLDER = "{vendor}";
private final DataSource dataSource;
public LocationResolver(DataSource dataSource) {
this.dataSource = dataSource;
}
public String[] resolveLocations(Collection<String> locations) {
return resolveLocations(locations.toArray(new String[locations.size()]));
}
public String[] resolveLocations(String[] locations) {
if (usesVendorLocation(locations)) {
DatabaseDriver databaseDriver = getDatabaseDriver();
return replaceVendorLocations(locations, databaseDriver);
}
return locations;
}
private String[] replaceVendorLocations(String[] locations,
DatabaseDriver databaseDriver) {
if (databaseDriver == DatabaseDriver.UNKNOWN) {
return locations;
}
String vendor = databaseDriver.getId();
return Arrays.stream(locations)
.map((location) -> location.replace(VENDOR_PLACEHOLDER, vendor))
.toArray(String[]::new);
}
private DatabaseDriver getDatabaseDriver() {
try {
String url = (String) JdbcUtils.extractDatabaseMetaData(this.dataSource,
"getURL");
return DatabaseDriver.fromJdbcUrl(url);
}
catch (MetaDataAccessException ex) {
throw new IllegalStateException(ex);
}
}
private boolean usesVendorLocation(String... locations) {
for (String location : locations) {
if (location.contains(VENDOR_PLACEHOLDER)) {
return true;
}
}
return false;
} }
} }
......
...@@ -258,11 +258,13 @@ public class FlywayAutoConfigurationTests { ...@@ -258,11 +258,13 @@ public class FlywayAutoConfigurationTests {
@Test @Test
public void useOneLocationWithVendorDirectory() { public void useOneLocationWithVendorDirectory() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.flyway.locations=classpath:db/vendors/{vendor}") .withPropertyValues(
"spring.flyway.locations=classpath:db/vendors/{vendor}")
.run((context) -> { .run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class); Flyway flyway = context.getBean(Flyway.class);
assertThat(flyway.getLocations()).containsExactly("classpath:db/vendors/h2"); assertThat(flyway.getLocations())
.containsExactly("classpath:db/vendors/h2");
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment