Skip to content

Commit

Permalink
Make FlywayMigrationInitializer ordered and public
Browse files Browse the repository at this point in the history
Update `FlywayAutoConfiguration` to allow explicit ordering of the
`FlywayMigrationInitializer`.

Fixes gh-4011
  • Loading branch information
philwebb committed Sep 24, 2015
1 parent 06f3202 commit 764c0a8
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 29 deletions.
Expand Up @@ -21,7 +21,6 @@
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;
Expand Down Expand Up @@ -120,6 +119,7 @@ else if (this.flywayDataSource != null) {
}

@Bean
@ConditionalOnMissingBean
public FlywayMigrationInitializer flywayInitializer(Flyway flyway) {
return new FlywayMigrationInitializer(flyway, this.migrationStrategy);

Expand All @@ -143,32 +143,4 @@ public FlywayJpaDependencyConfiguration() {

}

/**
* {@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;
}

}
Expand Up @@ -34,6 +34,7 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -161,6 +162,18 @@ public void customFlywayMigrationStrategy() throws Exception {
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) {
this.context.register(annotatedClasses);
this.context.refresh();
Expand All @@ -179,6 +192,18 @@ public DataSource flywayDataSource() {

}

@Configuration
protected static class CustomFlywayMigrationInitializer {

@Bean
public FlywayMigrationInitializer flywayMigrationInitializer(Flyway flyway) {
FlywayMigrationInitializer initializer = new FlywayMigrationInitializer(
flyway);
initializer.setOrder(Ordered.HIGHEST_PRECEDENCE);
return initializer;
}
}

@Component
protected static class MockFlywayMigrationStrategy implements FlywayMigrationStrategy {

Expand Down

0 comments on commit 764c0a8

Please sign in to comment.