Make FlywayMigrationInitializer ordered and public
Update `FlywayAutoConfiguration` to allow explicit ordering of the `FlywayMigrationInitializer`. Fixes gh-4011
This commit is contained in:
parent
06f3202c68
commit
764c0a8bf9
|
|
@ -21,7 +21,6 @@ import javax.persistence.EntityManagerFactory;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.flywaydb.core.Flyway;
|
import org.flywaydb.core.Flyway;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
|
@ -120,6 +119,7 @@ public class FlywayAutoConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
public FlywayMigrationInitializer flywayInitializer(Flyway flyway) {
|
public FlywayMigrationInitializer flywayInitializer(Flyway flyway) {
|
||||||
return new FlywayMigrationInitializer(flyway, this.migrationStrategy);
|
return new FlywayMigrationInitializer(flyway, this.migrationStrategy);
|
||||||
|
|
||||||
|
|
@ -143,32 +143,4 @@ public class FlywayAutoConfiguration {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@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;
|
|
||||||
|
|
||||||
FlywayMigrationInitializer(Flyway flyway,
|
|
||||||
FlywayMigrationStrategy migrationStrategy) {
|
|
||||||
this.flyway = flyway;
|
|
||||||
this.migrationStrategy = migrationStrategy;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterPropertiesSet() throws Exception {
|
|
||||||
if (this.migrationStrategy != null) {
|
|
||||||
this.migrationStrategy.migrate(this.flyway);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this.flyway.migrate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,78 @@
|
||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link InitializingBean} used to trigger {@link Flyway} migration via the
|
||||||
|
* {@link FlywayMigrationStrategy}.
|
||||||
|
*
|
||||||
|
* @author Phillip Webb
|
||||||
|
* @since 1.3.0
|
||||||
|
*/
|
||||||
|
public class FlywayMigrationInitializer implements InitializingBean, Ordered {
|
||||||
|
|
||||||
|
private final Flyway flyway;
|
||||||
|
|
||||||
|
private final FlywayMigrationStrategy migrationStrategy;
|
||||||
|
|
||||||
|
private int order = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link FlywayMigrationInitializer}
|
||||||
|
* @param flyway the flyway instance
|
||||||
|
*/
|
||||||
|
public FlywayMigrationInitializer(Flyway flyway) {
|
||||||
|
this(flyway, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link FlywayMigrationInitializer}
|
||||||
|
* @param flyway the flyway instance
|
||||||
|
* @param migrationStrategy the migration stragety or {@code null}
|
||||||
|
*/
|
||||||
|
public FlywayMigrationInitializer(Flyway flyway,
|
||||||
|
FlywayMigrationStrategy migrationStrategy) {
|
||||||
|
Assert.notNull(flyway, "Flyway must not be null");
|
||||||
|
this.flyway = flyway;
|
||||||
|
this.migrationStrategy = migrationStrategy;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() throws Exception {
|
||||||
|
if (this.migrationStrategy != null) {
|
||||||
|
this.migrationStrategy.migrate(this.flyway);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.flyway.migrate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return this.order;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrder(int order) {
|
||||||
|
this.order = order;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -34,6 +34,7 @@ import org.springframework.boot.test.EnvironmentTestUtils;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
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.Ordered;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
@ -161,6 +162,18 @@ public class FlywayAutoConfigurationTests {
|
||||||
this.context.getBean(MockFlywayMigrationStrategy.class).assertCalled();
|
this.context.getBean(MockFlywayMigrationStrategy.class).assertCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void customFlywayMigrationInitializer() throws Exception {
|
||||||
|
registerAndRefresh(CustomFlywayMigrationInitializer.class,
|
||||||
|
EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class,
|
||||||
|
PropertyPlaceholderAutoConfiguration.class);
|
||||||
|
assertNotNull(this.context.getBean(Flyway.class));
|
||||||
|
FlywayMigrationInitializer initializer = this.context
|
||||||
|
.getBean(FlywayMigrationInitializer.class);
|
||||||
|
assertThat(initializer.getOrder(), equalTo(Ordered.HIGHEST_PRECEDENCE));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private void registerAndRefresh(Class<?>... annotatedClasses) {
|
private void registerAndRefresh(Class<?>... annotatedClasses) {
|
||||||
this.context.register(annotatedClasses);
|
this.context.register(annotatedClasses);
|
||||||
this.context.refresh();
|
this.context.refresh();
|
||||||
|
|
@ -179,6 +192,18 @@ public class FlywayAutoConfigurationTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
protected static class CustomFlywayMigrationInitializer {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FlywayMigrationInitializer flywayMigrationInitializer(Flyway flyway) {
|
||||||
|
FlywayMigrationInitializer initializer = new FlywayMigrationInitializer(
|
||||||
|
flyway);
|
||||||
|
initializer.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
||||||
|
return initializer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
protected static class MockFlywayMigrationStrategy implements FlywayMigrationStrategy {
|
protected static class MockFlywayMigrationStrategy implements FlywayMigrationStrategy {
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue