Add FlywayMigrationStrategy support

Update FlywayAutoConfiguration to support pluggable migration
strategies. Rather than always calling flyway.migrate(), users can now
provide a FlywayMigrationStrategy @Bean to call whatever methods they
wish.

Fixes gh-1814
This commit is contained in:
Phillip Webb 2015-04-09 15:31:19 -07:00
parent e4aab1cf0d
commit ebccedcfdd
4 changed files with 112 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
@ -45,9 +45,9 @@ import org.springframework.util.StringUtils;
public class EntityManagerFactoryDependsOnPostProcessor implements
BeanFactoryPostProcessor {
private final String dependsOn;
private final String[] dependsOn;
public EntityManagerFactoryDependsOnPostProcessor(String dependsOn) {
public EntityManagerFactoryDependsOnPostProcessor(String... dependsOn) {
this.dependsOn = dependsOn;
}
@ -55,8 +55,11 @@ public class EntityManagerFactoryDependsOnPostProcessor implements
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
for (String beanName : getEntityManagerFactoryBeanNames(beanFactory)) {
BeanDefinition definition = getBeanDefinition(beanName, beanFactory);
definition.setDependsOn(StringUtils.addStringToArray(
definition.getDependsOn(), this.dependsOn));
String[] dependencies = definition.getDependsOn();
for (String bean : this.dependsOn) {
dependencies = StringUtils.addStringToArray(dependencies, bean);
}
definition.setDependsOn(dependencies);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
@ -21,6 +21,7 @@ import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -95,7 +96,13 @@ public class FlywayAutoConfiguration {
return false;
}
@Bean(initMethod = "migrate")
@Bean
@ConditionalOnMissingBean
public FlywayMigrationStrategy flywayMigrationStrategy() {
return new FlywayMigrationStrategy();
}
@Bean
@ConfigurationProperties(prefix = "flyway")
public Flyway flyway() {
Flyway flyway = new Flyway();
@ -113,6 +120,13 @@ public class FlywayAutoConfiguration {
return flyway;
}
@Bean
public FlywayMigrationInitializer flywayInitializer(Flyway flyway,
FlywayMigrationStrategy migrationStrategy) {
return new FlywayMigrationInitializer(flyway, migrationStrategy);
}
}
/**
@ -126,7 +140,30 @@ public class FlywayAutoConfiguration {
EntityManagerFactoryDependsOnPostProcessor {
public FlywayJpaDependencyConfiguration() {
super("flyway");
super("flywayInitializer", "flyway");
}
}
/**
* {@link InitializingBean} used to trigger {@link Flyway} migration via the
* {@link FlywayMigrationStrategy}.
*/
private static class FlywayMigrationInitializer implements InitializingBean {
private final Flyway flyway;
private final FlywayMigrationStrategy migrationStrategy;
public FlywayMigrationInitializer(Flyway flyway,
FlywayMigrationStrategy migrationStrategy) {
this.flyway = flyway;
this.migrationStrategy = migrationStrategy;
}
@Override
public void afterPropertiesSet() throws Exception {
this.migrationStrategy.migrate(this.flyway);
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2012-2015 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 org.flywaydb.core.Flyway;
/**
* Strategy used to initialize {@link Flyway} migration. Custom implementations may be
* registered as a {@code @Bean} to override the default migration behavior.
*
* @author Andreas Ahlenstorf
* @author Phillip Webb
*/
public class FlywayMigrationStrategy {
public void migrate(Flyway flyway) {
flyway.migrate();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
@ -34,9 +34,12 @@ import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link FlywayAutoConfiguration}.
@ -148,6 +151,16 @@ public class FlywayAutoConfigurationTests {
FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
}
@Test
public void customFlywayMigrationStrategy() throws Exception {
registerAndRefresh(EmbeddedDataSourceConfiguration.class,
FlywayAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
MockFlywayMigrationStrategy.class);
assertNotNull(this.context.getBean(Flyway.class));
this.context.getBean(MockFlywayMigrationStrategy.class).assertCalled();
}
private void registerAndRefresh(Class<?>... annotatedClasses) {
this.context.register(annotatedClasses);
this.context.refresh();
@ -165,4 +178,20 @@ public class FlywayAutoConfigurationTests {
}
}
@Component
protected static class MockFlywayMigrationStrategy extends FlywayMigrationStrategy {
private boolean called = false;
@Override
public void migrate(Flyway flyway) {
this.called = true;
}
public void assertCalled() {
assertThat(this.called, equalTo(true));
}
}
}