-
Notifications
You must be signed in to change notification settings - Fork 40.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add autoconfig support for Flyway migrations
Flyway starts up with its default settings if it is on the classpath. You can also ask Boot to barf if the migration scripts are missing. Fixes gh-730
- Loading branch information
Dave Syer
committed
May 2, 2014
1 parent
68e33b2
commit 5548b24
Showing
20 changed files
with
498 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
.../src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2012-2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.flyway; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.sql.DataSource; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.DefaultResourceLoader; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.util.Assert; | ||
|
||
import com.googlecode.flyway.core.Flyway; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for Flyway database migrations. | ||
* | ||
* @author Dave Syer | ||
*/ | ||
@Configuration | ||
@ConditionalOnClass(Flyway.class) | ||
@AutoConfigureAfter(DataSourceAutoConfiguration.class) | ||
public class FlywayAutoConfiguration { | ||
|
||
@Configuration | ||
@ConditionalOnMissingBean(Flyway.class) | ||
@EnableConfigurationProperties(FlywayProperties.class) | ||
public static class LiquibaseConfiguration { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
@Autowired | ||
private FlywayProperties properties = new FlywayProperties(); | ||
|
||
@Autowired | ||
private ResourceLoader resourceLoader = new DefaultResourceLoader(); | ||
|
||
@Autowired | ||
private DataSource dataSource; | ||
|
||
@PostConstruct | ||
public void checkLocationExists() { | ||
if (this.properties.isCheckLocation()) { | ||
|
||
Assert.state(!this.properties.getLocations().isEmpty(), | ||
"Migration script locations not configured"); | ||
boolean exists = false; | ||
for (String location : this.properties.getLocations()) { | ||
Resource resource = this.resourceLoader.getResource(location); | ||
exists = !exists && resource.exists(); | ||
} | ||
Assert.state(exists, "Cannot find migrations location in: " | ||
+ this.properties.getLocations() | ||
+ " (please add migrations or check your Flyway configuration)"); | ||
} | ||
} | ||
|
||
@Bean | ||
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; | ||
} | ||
|
||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
...nfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2012-2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.flyway; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
/** | ||
* Configuration properties to configure Flyway. | ||
* | ||
* @author Dave Syer | ||
*/ | ||
@ConfigurationProperties(prefix = "spring.flyway", ignoreUnknownFields = false) | ||
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 boolean checkLocation; | ||
|
||
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; | ||
} | ||
|
||
public List<String> getLocations() { | ||
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; | ||
} | ||
|
||
public boolean isCheckLocation() { | ||
return this.checkLocation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2012-2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.flyway; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.junit.After; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.springframework.beans.factory.BeanCreationException; | ||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; | ||
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; | ||
import org.springframework.boot.test.EnvironmentTestUtils; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
|
||
import com.googlecode.flyway.core.Flyway; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Tests for {@link LiquibaseAutoConfiguration}. | ||
* | ||
* @author Dave Syer | ||
*/ | ||
public class FlywayAutoConfigurationTests { | ||
|
||
@Rule | ||
public ExpectedException expected = ExpectedException.none(); | ||
|
||
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();; | ||
|
||
@After | ||
public void close() { | ||
if (this.context != null) { | ||
this.context.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testNoDataSource() throws Exception { | ||
this.context.register(FlywayAutoConfiguration.class, | ||
PropertyPlaceholderAutoConfiguration.class); | ||
this.expected.expect(BeanCreationException.class); | ||
this.expected.expectMessage("No qualifying bean"); | ||
this.expected.expectMessage("DataSource"); | ||
this.context.refresh(); | ||
} | ||
|
||
@Test | ||
public void testDefaultFlyway() throws Exception { | ||
this.context | ||
.register(EmbeddedDataSourceConfiguration.class, | ||
FlywayAutoConfiguration.class, | ||
PropertyPlaceholderAutoConfiguration.class); | ||
this.context.refresh(); | ||
Flyway flyway = this.context.getBean(Flyway.class); | ||
assertEquals("[classpath:db/migrations]", Arrays.asList(flyway.getLocations()) | ||
.toString()); | ||
} | ||
|
||
@Test | ||
public void testOverrideLocations() throws Exception { | ||
EnvironmentTestUtils.addEnvironment(this.context, | ||
"spring.flyway.locations:classpath:db/changelog"); | ||
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()); | ||
} | ||
|
||
@Test | ||
public void testOverrideSchemas() throws Exception { | ||
EnvironmentTestUtils.addEnvironment(this.context, "spring.flyway.schemas:public"); | ||
this.context | ||
.register(EmbeddedDataSourceConfiguration.class, | ||
FlywayAutoConfiguration.class, | ||
PropertyPlaceholderAutoConfiguration.class); | ||
this.context.refresh(); | ||
Flyway flyway = this.context.getBean(Flyway.class); | ||
assertEquals("[public]", Arrays.asList(flyway.getSchemas()).toString()); | ||
} | ||
|
||
@Test(expected = BeanCreationException.class) | ||
public void testChangeLogDoesNotExist() throws Exception { | ||
EnvironmentTestUtils.addEnvironment(this.context, | ||
"spring.flyway.locations:no-such-dir"); | ||
this.context | ||
.register(EmbeddedDataSourceConfiguration.class, | ||
FlywayAutoConfiguration.class, | ||
PropertyPlaceholderAutoConfiguration.class); | ||
this.context.refresh(); | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Liquibase configuration should probably be renamed here.