Add support for vendor specific Flyway migration locations

This commit adds support for using `{vendor}` placeholder in
`flyway.locations` configuration property value.

See gh-6900
pull/7763/head
Vedran Pavic 8 years ago committed by Stephane Nicoll
parent 62bff1017e
commit 65c2d1f5e7

@ -40,11 +40,14 @@ import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfigurat
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding; import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter; import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.jdbc.support.MetaDataAccessException;
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean; import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -79,6 +82,8 @@ public class FlywayAutoConfiguration {
@EnableConfigurationProperties(FlywayProperties.class) @EnableConfigurationProperties(FlywayProperties.class)
public static class FlywayConfiguration { public static class FlywayConfiguration {
private static final String VENDOR_PLACEHOLDER = "{vendor}";
private final FlywayProperties properties; private final FlywayProperties properties;
private final ResourceLoader resourceLoader; private final ResourceLoader resourceLoader;
@ -126,7 +131,31 @@ public class FlywayAutoConfiguration {
@Bean @Bean
@ConfigurationProperties(prefix = "flyway") @ConfigurationProperties(prefix = "flyway")
public Flyway flyway() { public Flyway flyway() {
Flyway flyway = new Flyway(); Flyway flyway = new Flyway() {
@Override
public void setLocations(String... locations) {
if (usesVendorLocation()) {
try {
String url = (String) JdbcUtils.extractDatabaseMetaData(
getDataSource(), "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.name().toLowerCase());
}
}
}
catch (MetaDataAccessException e) {
throw new IllegalStateException(e);
}
}
super.setLocations(locations);
}
};
if (this.properties.isCreateDataSource()) { if (this.properties.isCreateDataSource()) {
flyway.setDataSource(this.properties.getUrl(), this.properties.getUser(), flyway.setDataSource(this.properties.getUrl(), this.properties.getUser(),
this.properties.getPassword(), this.properties.getPassword(),
@ -142,6 +171,15 @@ public class FlywayAutoConfiguration {
return flyway; return flyway;
} }
private boolean usesVendorLocation() {
for (String location : this.properties.getLocations()) {
if (location.contains(VENDOR_PLACEHOLDER)) {
return true;
}
}
return false;
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public FlywayMigrationInitializer flywayInitializer(Flyway flyway) { public FlywayMigrationInitializer flywayInitializer(Flyway flyway) {

@ -234,6 +234,18 @@ public class FlywayAutoConfigurationTests {
.isEqualTo(MigrationVersion.fromVersion("1")); .isEqualTo(MigrationVersion.fromVersion("1"));
} }
@Test
public void useVendorDirectory() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
"flyway.locations:classpath:db/vendors/{vendor}");
registerAndRefresh(EmbeddedDataSourceConfiguration.class,
FlywayAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
Flyway flyway = this.context.getBean(Flyway.class);
assertThat(flyway.getLocations()).containsExactly(
"classpath:db/vendors/h2");
}
private void registerAndRefresh(Class<?>... annotatedClasses) { private void registerAndRefresh(Class<?>... annotatedClasses) {
this.context.register(annotatedClasses); this.context.register(annotatedClasses);
this.context.refresh(); this.context.refresh();

Loading…
Cancel
Save