Use Flyway to bind flyway.*

Since Flyway has bean properties (with getters and setters)
it can be used to bin directly to the Environment (instead of
copying all the properties into FlywayProperties).

Fixes gh-806
pull/931/head
Dave Syer 11 years ago
parent 89e0b3d552
commit e118515d7a

@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -80,16 +81,11 @@ public class FlywayAutoConfiguration {
}
}
@Bean
@Bean(initMethod = "migrate")
@ConfigurationProperties(prefix = "flyway")
public Flyway flyway(DataSource dataSource) {
Flyway flyway = new Flyway();
flyway.setLocations(this.properties.getLocations().toArray(new String[0]));
flyway.setSchemas(this.properties.getSchemas().toArray(new String[0]));
flyway.setInitVersion(this.properties.getInitVersion());
flyway.setSqlMigrationPrefix(this.properties.getPrefix());
flyway.setSqlMigrationSuffix(this.properties.getSuffix());
flyway.setDataSource(dataSource);
flyway.migrate();
return flyway;
}

@ -16,60 +16,31 @@
package org.springframework.boot.autoconfigure.flyway;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.flywaydb.core.Flyway;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Configuration properties for Flyway database migrations.
* Configuration properties for Flyway database migrations. These are only the properties
* that Spring needs to validate and enable the migrations. If you want to control the
* location or format of the scripts you can use the same prefix ("flyway") to inject
* properties into the {@link Flyway} instance.
*
* @author Dave Syer
*
* @since 1.1.0
*/
@ConfigurationProperties(prefix = "flyway", ignoreUnknownFields = false)
@ConfigurationProperties(prefix = "flyway", ignoreUnknownFields = true)
public class FlywayProperties {
private List<String> locations = Arrays.asList("db/migrations");
private List<String> schemas = new ArrayList<String>();
private String prefix = "V";
private String suffix = ".sql";
private String initVersion = "1";
private List<String> locations = Arrays.asList("db/migration");
private boolean checkLocation = false;
private boolean enabled = true;
public String getPrefix() {
return this.prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return this.suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public String getInitVersion() {
return this.initVersion;
}
public void setInitVersion(String initVersion) {
this.initVersion = initVersion;
}
public void setLocations(List<String> locations) {
this.locations = locations;
}
@ -78,14 +49,6 @@ public class FlywayProperties {
return this.locations;
}
public List<String> getSchemas() {
return this.schemas;
}
public void setSchemas(List<String> schemas) {
this.schemas = schemas;
}
public void setCheckLocation(boolean checkLocation) {
this.checkLocation = checkLocation;
}

@ -74,22 +74,22 @@ public class FlywayAutoConfigurationTests {
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
Flyway flyway = this.context.getBean(Flyway.class);
assertEquals("[classpath:db/migrations]", Arrays.asList(flyway.getLocations())
assertEquals("[classpath:db/migration]", Arrays.asList(flyway.getLocations())
.toString());
}
@Test
public void testOverrideLocations() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
"flyway.locations:classpath:db/changelog");
"flyway.locations:classpath:db/changelog,classpath:db/migration");
this.context
.register(EmbeddedDataSourceConfiguration.class,
FlywayAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
Flyway flyway = this.context.getBean(Flyway.class);
assertEquals("[classpath:db/changelog]", Arrays.asList(flyway.getLocations())
.toString());
assertEquals("[classpath:db/changelog, classpath:db/migration]",
Arrays.asList(flyway.getLocations()).toString());
}
@Test

@ -1207,10 +1207,12 @@ To automatically run Flyway database migrations on startup, add the
The migrations are scripts in the form `V<VERSION>__<NAME>.sql` (with
`<VERSION>` an underscore-separated version, e.g. "1" or "2_1"). By
default they live in a folder `classpath:db/migrations` but you can
modify that using `flyway.locations` (a list). See
default they live in a folder `classpath:db/migration` but you can
modify that using `flyway.locations` (a list). See the Flyway class from
flyway-core for details of available settings like schemas etc. In
addition Spring Boot provides a small set of properties in
{sc-spring-boot-autoconfigure}/flyway/FlywayProperties.{sc-ext}[`FlywayProperties`]
for details of available settings like schemas etc.
that can be used to disable the migrations, or switch off the location checking.
There is a {github-code}/spring-boot-samples/spring-boot-sample-flyway[Flyway sample] so
you can see how to set things up.

@ -1,2 +1 @@
spring.jpa.generate-ddl: false
spring.jpa.hibernate.ddl-auto: validate
Loading…
Cancel
Save