Skip to content

Commit 95ccd76

Browse files
committed
Merge pull request #5344 from Eddú Meléndez
* gh-5344: Polish "Upgrade to Flyway 4.0" Upgrade to Flyway 4.0
2 parents 15639d3 + 4ea7dc6 commit 95ccd76

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.util.Collections;
2020
import java.util.HashSet;
21+
import java.util.List;
2122
import java.util.Set;
2223

2324
import javax.annotation.PostConstruct;
@@ -26,6 +27,7 @@
2627

2728
import org.flywaydb.core.Flyway;
2829
import org.flywaydb.core.api.MigrationVersion;
30+
import org.flywaydb.core.api.callback.FlywayCallback;
2931

3032
import org.springframework.beans.factory.ObjectProvider;
3133
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@@ -61,6 +63,7 @@
6163
* @author Vedran Pavic
6264
* @author Stephane Nicoll
6365
* @author Jacques-Etienne Beaudet
66+
* @author Eddú Meléndez
6467
* @since 1.1.0
6568
*/
6669
@Configuration
@@ -92,15 +95,20 @@ public static class FlywayConfiguration {
9295

9396
private final FlywayMigrationStrategy migrationStrategy;
9497

98+
private List<FlywayCallback> flywayCallbacks;
99+
95100
public FlywayConfiguration(FlywayProperties properties,
96101
ResourceLoader resourceLoader, ObjectProvider<DataSource> dataSource,
97102
@FlywayDataSource ObjectProvider<DataSource> flywayDataSource,
98-
ObjectProvider<FlywayMigrationStrategy> migrationStrategy) {
103+
ObjectProvider<FlywayMigrationStrategy> migrationStrategy,
104+
ObjectProvider<List<FlywayCallback>> flywayCallbacks) {
99105
this.properties = properties;
100106
this.resourceLoader = resourceLoader;
101107
this.dataSource = dataSource.getIfUnique();
102108
this.flywayDataSource = flywayDataSource.getIfAvailable();
103109
this.migrationStrategy = migrationStrategy.getIfAvailable();
110+
this.flywayCallbacks = flywayCallbacks
111+
.getIfAvailable(() -> Collections.emptyList());
104112
}
105113

106114
@PostConstruct
@@ -140,6 +148,8 @@ else if (this.flywayDataSource != null) {
140148
else {
141149
flyway.setDataSource(this.dataSource);
142150
}
151+
flyway.setCallbacks(this.flywayCallbacks
152+
.toArray(new FlywayCallback[this.flywayCallbacks.size()]));
143153
flyway.setLocations(this.properties.getLocations().toArray(new String[0]));
144154
return flyway;
145155
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure.flyway;
1818

19+
import java.sql.Connection;
1920
import java.util.Arrays;
2021
import java.util.Collections;
2122
import java.util.HashMap;
@@ -25,12 +26,14 @@
2526

2627
import org.flywaydb.core.Flyway;
2728
import org.flywaydb.core.api.MigrationVersion;
29+
import org.flywaydb.core.api.callback.FlywayCallback;
2830
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
2931
import org.junit.After;
3032
import org.junit.Before;
3133
import org.junit.Rule;
3234
import org.junit.Test;
3335
import org.junit.rules.ExpectedException;
36+
import org.mockito.InOrder;
3437

3538
import org.springframework.beans.factory.BeanCreationException;
3639
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
@@ -43,12 +46,16 @@
4346
import org.springframework.context.annotation.Configuration;
4447
import org.springframework.context.annotation.Primary;
4548
import org.springframework.core.Ordered;
49+
import org.springframework.core.annotation.Order;
4650
import org.springframework.core.env.MapPropertySource;
4751
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
4852
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
4953
import org.springframework.stereotype.Component;
5054

5155
import static org.assertj.core.api.Assertions.assertThat;
56+
import static org.mockito.ArgumentMatchers.any;
57+
import static org.mockito.Mockito.inOrder;
58+
import static org.mockito.Mockito.mock;
5259

5360
/**
5461
* Tests for {@link FlywayAutoConfiguration}.
@@ -57,6 +64,7 @@
5764
* @author Phillip Webb
5865
* @author Andy Wilkinson
5966
* @author Vedran Pavic
67+
* @author Eddú Meléndez
6068
*/
6169
public class FlywayAutoConfigurationTests {
6270

@@ -246,6 +254,23 @@ public void useVendorDirectory() throws Exception {
246254
"classpath:db/vendors/h2", "classpath:db/changelog");
247255
}
248256

257+
@Test
258+
public void callbacksAreConfiguredAndOrdered() throws Exception {
259+
registerAndRefresh(EmbeddedDataSourceConfiguration.class,
260+
FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
261+
CallbackConfiguration.class);
262+
assertThat(this.context.getBeansOfType(Flyway.class)).hasSize(1);
263+
Flyway flyway = this.context.getBean(Flyway.class);
264+
FlywayCallback callbackOne = this.context.getBean("callbackOne",
265+
FlywayCallback.class);
266+
FlywayCallback callbackTwo = this.context.getBean("callbackTwo",
267+
FlywayCallback.class);
268+
assertThat(flyway.getCallbacks()).containsExactly(callbackTwo, callbackOne);
269+
InOrder orderedCallbacks = inOrder(callbackOne, callbackTwo);
270+
orderedCallbacks.verify(callbackTwo).beforeMigrate(any(Connection.class));
271+
orderedCallbacks.verify(callbackOne).beforeMigrate(any(Connection.class));
272+
}
273+
249274
private void registerAndRefresh(Class<?>... annotatedClasses) {
250275
this.context.register(annotatedClasses);
251276
this.context.refresh();
@@ -325,4 +350,21 @@ public void assertCalled() {
325350

326351
}
327352

353+
@Configuration
354+
static class CallbackConfiguration {
355+
356+
@Bean
357+
@Order(1)
358+
public FlywayCallback callbackOne() {
359+
return mock(FlywayCallback.class);
360+
}
361+
362+
@Bean
363+
@Order(0)
364+
public FlywayCallback callbackTwo() {
365+
return mock(FlywayCallback.class);
366+
}
367+
368+
}
369+
328370
}

spring-boot-dependencies/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<ehcache.version>2.10.3</ehcache.version>
7474
<ehcache3.version>3.2.0</ehcache3.version>
7575
<embedded-mongo.version>1.50.5</embedded-mongo.version>
76-
<flyway.version>3.2.1</flyway.version>
76+
<flyway.version>4.0.3</flyway.version>
7777
<freemarker.version>2.3.25-incubating</freemarker.version>
7878
<elasticsearch.version>2.4.4</elasticsearch.version>
7979
<glassfish-el.version>3.0.0</glassfish-el.version>

spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,8 +1952,12 @@ Boot will call `Flyway.migrate()` to perform the database migration. If you woul
19521952
more control, provide a `@Bean` that implements
19531953
{sc-spring-boot-autoconfigure}/flyway/FlywayMigrationStrategy.{sc-ext}[`FlywayMigrationStrategy`].
19541954

1955-
TIP: If you want to make use of http://flywaydb.org/documentation/callbacks.html[Flyway
1956-
callbacks], those scripts should also live in the `classpath:db/migration` folder.
1955+
Flyway supports SQL and Java http://flywaydb.org/documentation/callbacks.html[callbacks].
1956+
To use SQL-based callbacks, place the callback scripts in the `classpath:db/migration`
1957+
folder. To use Java-based callbacks, create one or more beans that implement
1958+
`FlywayCallback` or, preferably, extend `BaseFlywayCallback`. Any such beans will be
1959+
automatically registered with `Flyway`. They can be ordered using `@Order` or by
1960+
implementing `Ordered`.
19571961

19581962
By default Flyway will autowire the (`@Primary`) `DataSource` in your context and
19591963
use that for migrations. If you like to use a different `DataSource` you can create

0 commit comments

Comments
 (0)