From e118515d7a623ed202fc491d2547171d3386c95a Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 20 May 2014 11:28:56 +0100 Subject: [PATCH] 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 --- .../flyway/FlywayAutoConfiguration.java | 10 ++-- .../flyway/FlywayProperties.java | 51 +++---------------- .../flyway/FlywayAutoConfigurationTests.java | 8 +-- .../db/{migrations => migration}/V1__init.sql | 0 spring-boot-docs/src/main/asciidoc/howto.adoc | 8 +-- .../src/main/resources/application.properties | 1 - .../db/{migrations => migration}/V1__init.sql | 0 7 files changed, 19 insertions(+), 59 deletions(-) rename spring-boot-autoconfigure/src/test/resources/db/{migrations => migration}/V1__init.sql (100%) rename spring-boot-samples/spring-boot-sample-flyway/src/main/resources/db/{migrations => migration}/V1__init.sql (100%) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java index e4ad638d90f..f25fd81c83a 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java @@ -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; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java index 1bf910046e4..4b0a33f5679 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java @@ -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 locations = Arrays.asList("db/migrations"); - - private List schemas = new ArrayList(); - - private String prefix = "V"; - - private String suffix = ".sql"; - - private String initVersion = "1"; + private List 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 locations) { this.locations = locations; } @@ -78,14 +49,6 @@ public class FlywayProperties { return this.locations; } - public List getSchemas() { - return this.schemas; - } - - public void setSchemas(List schemas) { - this.schemas = schemas; - } - public void setCheckLocation(boolean checkLocation) { this.checkLocation = checkLocation; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java index 088c0ac9dbd..72a17e23b0d 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java @@ -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 diff --git a/spring-boot-autoconfigure/src/test/resources/db/migrations/V1__init.sql b/spring-boot-autoconfigure/src/test/resources/db/migration/V1__init.sql similarity index 100% rename from spring-boot-autoconfigure/src/test/resources/db/migrations/V1__init.sql rename to spring-boot-autoconfigure/src/test/resources/db/migration/V1__init.sql diff --git a/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-docs/src/main/asciidoc/howto.adoc index 60da5cef0a8..eb152c0aef3 100644 --- a/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -1207,10 +1207,12 @@ To automatically run Flyway database migrations on startup, add the The migrations are scripts in the form `V__.sql` (with `` 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. diff --git a/spring-boot-samples/spring-boot-sample-flyway/src/main/resources/application.properties b/spring-boot-samples/spring-boot-sample-flyway/src/main/resources/application.properties index 56a72176654..2cbca2b524c 100644 --- a/spring-boot-samples/spring-boot-sample-flyway/src/main/resources/application.properties +++ b/spring-boot-samples/spring-boot-sample-flyway/src/main/resources/application.properties @@ -1,2 +1 @@ -spring.jpa.generate-ddl: false spring.jpa.hibernate.ddl-auto: validate \ No newline at end of file diff --git a/spring-boot-samples/spring-boot-sample-flyway/src/main/resources/db/migrations/V1__init.sql b/spring-boot-samples/spring-boot-sample-flyway/src/main/resources/db/migration/V1__init.sql similarity index 100% rename from spring-boot-samples/spring-boot-sample-flyway/src/main/resources/db/migrations/V1__init.sql rename to spring-boot-samples/spring-boot-sample-flyway/src/main/resources/db/migration/V1__init.sql