Upgrade to Flyway 6.0.1

See gh-17997
This commit is contained in:
Semyon Danilov 2019-03-15 21:41:01 +03:00 committed by Andy Wilkinson
parent 1e2f8959d9
commit 278b20d9c9
4 changed files with 37 additions and 86 deletions

View File

@ -30,7 +30,6 @@ import javax.sql.DataSource;
import org.flywaydb.core.Flyway; import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationVersion; import org.flywaydb.core.api.MigrationVersion;
import org.flywaydb.core.api.callback.Callback; import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.callback.FlywayCallback;
import org.flywaydb.core.api.configuration.FluentConfiguration; import org.flywaydb.core.api.configuration.FluentConfiguration;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
@ -81,6 +80,7 @@ import org.springframework.util.StringUtils;
* @author Dominic Gunn * @author Dominic Gunn
* @author Dan Zheng * @author Dan Zheng
* @author András Deák * @author András Deák
* @author Semyon Danilov
* @since 1.1.0 * @since 1.1.0
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@ -113,7 +113,7 @@ public class FlywayAutoConfiguration {
ResourceLoader resourceLoader, ObjectProvider<DataSource> dataSource, ResourceLoader resourceLoader, ObjectProvider<DataSource> dataSource,
@FlywayDataSource ObjectProvider<DataSource> flywayDataSource, @FlywayDataSource ObjectProvider<DataSource> flywayDataSource,
ObjectProvider<FlywayConfigurationCustomizer> fluentConfigurationCustomizers, ObjectProvider<FlywayConfigurationCustomizer> fluentConfigurationCustomizers,
ObjectProvider<Callback> callbacks, ObjectProvider<FlywayCallback> flywayCallbacks) { ObjectProvider<Callback> callbacks) {
FluentConfiguration configuration = new FluentConfiguration(resourceLoader.getClassLoader()); FluentConfiguration configuration = new FluentConfiguration(resourceLoader.getClassLoader());
DataSource dataSourceToMigrate = configureDataSource(configuration, properties, dataSourceProperties, DataSource dataSourceToMigrate = configureDataSource(configuration, properties, dataSourceProperties,
flywayDataSource.getIfAvailable(), dataSource.getIfAvailable()); flywayDataSource.getIfAvailable(), dataSource.getIfAvailable());
@ -122,10 +122,8 @@ public class FlywayAutoConfiguration {
List<Callback> orderedCallbacks = callbacks.orderedStream().collect(Collectors.toList()); List<Callback> orderedCallbacks = callbacks.orderedStream().collect(Collectors.toList());
configureCallbacks(configuration, orderedCallbacks); configureCallbacks(configuration, orderedCallbacks);
fluentConfigurationCustomizers.orderedStream().forEach((customizer) -> customizer.customize(configuration)); fluentConfigurationCustomizers.orderedStream().forEach((customizer) -> customizer.customize(configuration));
Flyway flyway = configuration.load(); configureFlywayCallbacks(configuration, orderedCallbacks);
List<FlywayCallback> orderedFlywayCallbacks = flywayCallbacks.orderedStream().collect(Collectors.toList()); return configuration.load();
configureFlywayCallbacks(flyway, orderedCallbacks, orderedFlywayCallbacks);
return flyway;
} }
private DataSource configureDataSource(FluentConfiguration configuration, FlywayProperties properties, private DataSource configureDataSource(FluentConfiguration configuration, FlywayProperties properties,
@ -210,14 +208,9 @@ public class FlywayAutoConfiguration {
} }
} }
private void configureFlywayCallbacks(Flyway flyway, List<Callback> callbacks, private void configureFlywayCallbacks(FluentConfiguration flyway, List<Callback> callbacks) {
List<FlywayCallback> flywayCallbacks) { if (!callbacks.isEmpty()) {
if (!flywayCallbacks.isEmpty()) { flyway.callbacks(callbacks.toArray(new Callback[0]));
if (!callbacks.isEmpty()) {
throw new IllegalStateException("Found a mixture of Callback and FlywayCallback beans."
+ " One type must be used exclusively.");
}
flyway.setCallbacks(flywayCallbacks.toArray(new FlywayCallback[0]));
} }
} }

View File

@ -16,7 +16,6 @@
package org.springframework.boot.autoconfigure.flyway; package org.springframework.boot.autoconfigure.flyway;
import java.sql.Connection;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -30,7 +29,6 @@ import org.flywaydb.core.api.MigrationVersion;
import org.flywaydb.core.api.callback.Callback; import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.callback.Context; import org.flywaydb.core.api.callback.Context;
import org.flywaydb.core.api.callback.Event; import org.flywaydb.core.api.callback.Event;
import org.flywaydb.core.api.callback.FlywayCallback;
import org.flywaydb.core.internal.license.FlywayProUpgradeRequiredException; import org.flywaydb.core.internal.license.FlywayProUpgradeRequiredException;
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform; import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -89,7 +87,7 @@ class FlywayAutoConfigurationTests {
this.contextRunner.withPropertyValues("spring.flyway.url:jdbc:hsqldb:mem:" + UUID.randomUUID()) this.contextRunner.withPropertyValues("spring.flyway.url:jdbc:hsqldb:mem:" + UUID.randomUUID())
.run((context) -> { .run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
assertThat(context.getBean(Flyway.class).getDataSource()).isNotNull(); assertThat(context.getBean(Flyway.class).getConfiguration().getDataSource()).isNotNull();
}); });
} }
@ -98,7 +96,7 @@ class FlywayAutoConfigurationTests {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.flyway.url:jdbc:hsqldb:mem:flywaytest").run((context) -> { .withPropertyValues("spring.flyway.url:jdbc:hsqldb:mem:flywaytest").run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
assertThat(context.getBean(Flyway.class).getDataSource()).isNotNull(); assertThat(context.getBean(Flyway.class).getConfiguration().getDataSource()).isNotNull();
}); });
} }
@ -109,7 +107,7 @@ class FlywayAutoConfigurationTests {
"spring.flyway.user:sa") "spring.flyway.user:sa")
.run((context) -> { .run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
assertThat(context.getBean(Flyway.class).getDataSource()).isNotNull(); assertThat(context.getBean(Flyway.class).getConfiguration().getDataSource()).isNotNull();
}); });
} }
@ -118,7 +116,7 @@ class FlywayAutoConfigurationTests {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.flyway.url:jdbc:hsqldb:mem:flywaytest").run((context) -> { .withPropertyValues("spring.flyway.url:jdbc:hsqldb:mem:flywaytest").run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
DataSource dataSource = context.getBean(Flyway.class).getDataSource(); DataSource dataSource = context.getBean(Flyway.class).getConfiguration().getDataSource();
assertThat(dataSource).isNotNull(); assertThat(dataSource).isNotNull();
assertThat(dataSource).hasFieldOrPropertyWithValue("user", "sa"); assertThat(dataSource).hasFieldOrPropertyWithValue("user", "sa");
assertThat(dataSource).hasFieldOrPropertyWithValue("password", ""); assertThat(dataSource).hasFieldOrPropertyWithValue("password", "");
@ -130,7 +128,7 @@ class FlywayAutoConfigurationTests {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.flyway.user:sa").run((context) -> { .withPropertyValues("spring.flyway.user:sa").run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
DataSource dataSource = context.getBean(Flyway.class).getDataSource(); DataSource dataSource = context.getBean(Flyway.class).getConfiguration().getDataSource();
assertThat(dataSource).isNotNull(); assertThat(dataSource).isNotNull();
assertThat(dataSource).extracting("url").asString().startsWith("jdbc:h2:mem:"); assertThat(dataSource).extracting("url").asString().startsWith("jdbc:h2:mem:");
}); });
@ -142,7 +140,7 @@ class FlywayAutoConfigurationTests {
.withUserConfiguration(FlywayDataSourceConfiguration.class, EmbeddedDataSourceConfiguration.class) .withUserConfiguration(FlywayDataSourceConfiguration.class, EmbeddedDataSourceConfiguration.class)
.run((context) -> { .run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
assertThat(context.getBean(Flyway.class).getDataSource()) assertThat(context.getBean(Flyway.class).getConfiguration().getDataSource())
.isEqualTo(context.getBean("flywayDataSource")); .isEqualTo(context.getBean("flywayDataSource"));
}); });
} }
@ -151,7 +149,8 @@ class FlywayAutoConfigurationTests {
void flywayDataSourceWithoutDataSourceAutoConfiguration() { void flywayDataSourceWithoutDataSourceAutoConfiguration() {
this.contextRunner.withUserConfiguration(FlywayDataSourceConfiguration.class).run((context) -> { this.contextRunner.withUserConfiguration(FlywayDataSourceConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
assertThat(context.getBean(Flyway.class).getDataSource()).isEqualTo(context.getBean("flywayDataSource")); assertThat(context.getBean(Flyway.class).getConfiguration().getDataSource())
.isEqualTo(context.getBean("flywayDataSource"));
}); });
} }
@ -175,7 +174,8 @@ class FlywayAutoConfigurationTests {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class).run((context) -> { this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class); Flyway flyway = context.getBean(Flyway.class);
assertThat(flyway.getLocations()).containsExactly(new Location("classpath:db/migration")); assertThat(flyway.getConfiguration().getLocations())
.containsExactly(new Location("classpath:db/migration"));
}); });
} }
@ -186,8 +186,8 @@ class FlywayAutoConfigurationTests {
.run((context) -> { .run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class); Flyway flyway = context.getBean(Flyway.class);
assertThat(flyway.getLocations()).containsExactly(new Location("classpath:db/changelog"), assertThat(flyway.getConfiguration().getLocations()).containsExactly(
new Location("classpath:db/migration")); new Location("classpath:db/changelog"), new Location("classpath:db/migration"));
}); });
} }
@ -199,8 +199,8 @@ class FlywayAutoConfigurationTests {
.run((context) -> { .run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class); Flyway flyway = context.getBean(Flyway.class);
assertThat(flyway.getLocations()).containsExactly(new Location("classpath:db/changelog"), assertThat(flyway.getConfiguration().getLocations()).containsExactly(
new Location("classpath:db/migration")); new Location("classpath:db/changelog"), new Location("classpath:db/migration"));
}); });
} }
@ -210,7 +210,7 @@ class FlywayAutoConfigurationTests {
.withPropertyValues("spring.flyway.schemas:public").run((context) -> { .withPropertyValues("spring.flyway.schemas:public").run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class); Flyway flyway = context.getBean(Flyway.class);
assertThat(Arrays.asList(flyway.getSchemas()).toString()).isEqualTo("[public]"); assertThat(Arrays.asList(flyway.getConfiguration().getSchemas()).toString()).isEqualTo("[public]");
}); });
} }
@ -289,7 +289,8 @@ class FlywayAutoConfigurationTests {
.withPropertyValues("spring.flyway.baseline-version=0").run((context) -> { .withPropertyValues("spring.flyway.baseline-version=0").run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class); Flyway flyway = context.getBean(Flyway.class);
assertThat(flyway.getBaselineVersion()).isEqualTo(MigrationVersion.fromVersion("0")); assertThat(flyway.getConfiguration().getBaselineVersion())
.isEqualTo(MigrationVersion.fromVersion("0"));
}); });
} }
@ -299,7 +300,8 @@ class FlywayAutoConfigurationTests {
.withPropertyValues("spring.flyway.baseline-version=1").run((context) -> { .withPropertyValues("spring.flyway.baseline-version=1").run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class); Flyway flyway = context.getBean(Flyway.class);
assertThat(flyway.getBaselineVersion()).isEqualTo(MigrationVersion.fromVersion("1")); assertThat(flyway.getConfiguration().getBaselineVersion())
.isEqualTo(MigrationVersion.fromVersion("1"));
}); });
} }
@ -310,8 +312,8 @@ class FlywayAutoConfigurationTests {
.run((context) -> { .run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class); Flyway flyway = context.getBean(Flyway.class);
assertThat(flyway.getLocations()).containsExactlyInAnyOrder(new Location("classpath:db/vendors/h2"), assertThat(flyway.getConfiguration().getLocations()).containsExactlyInAnyOrder(
new Location("classpath:db/changelog")); new Location("classpath:db/vendors/h2"), new Location("classpath:db/changelog"));
}); });
} }
@ -321,7 +323,8 @@ class FlywayAutoConfigurationTests {
.withPropertyValues("spring.flyway.locations=classpath:db/vendors/{vendor}").run((context) -> { .withPropertyValues("spring.flyway.locations=classpath:db/vendors/{vendor}").run((context) -> {
assertThat(context).hasSingleBean(Flyway.class); assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class); Flyway flyway = context.getBean(Flyway.class);
assertThat(flyway.getLocations()).containsExactly(new Location("classpath:db/vendors/h2")); assertThat(flyway.getConfiguration().getLocations())
.containsExactly(new Location("classpath:db/vendors/h2"));
}); });
} }
@ -333,41 +336,14 @@ class FlywayAutoConfigurationTests {
Flyway flyway = context.getBean(Flyway.class); Flyway flyway = context.getBean(Flyway.class);
Callback callbackOne = context.getBean("callbackOne", Callback.class); Callback callbackOne = context.getBean("callbackOne", Callback.class);
Callback callbackTwo = context.getBean("callbackTwo", Callback.class); Callback callbackTwo = context.getBean("callbackTwo", Callback.class);
assertThat(flyway.getCallbacks()).hasSize(2); assertThat(flyway.getConfiguration().getCallbacks()).hasSize(2);
assertThat(flyway.getCallbacks()).containsExactly(callbackTwo, callbackOne); assertThat(flyway.getConfiguration().getCallbacks()).containsExactly(callbackTwo, callbackOne);
InOrder orderedCallbacks = inOrder(callbackOne, callbackTwo); InOrder orderedCallbacks = inOrder(callbackOne, callbackTwo);
orderedCallbacks.verify(callbackTwo).handle(any(Event.class), any(Context.class)); orderedCallbacks.verify(callbackTwo).handle(any(Event.class), any(Context.class));
orderedCallbacks.verify(callbackOne).handle(any(Event.class), any(Context.class)); orderedCallbacks.verify(callbackOne).handle(any(Event.class), any(Context.class));
}); });
} }
@Test
void legacyCallbacksAreConfiguredAndOrdered() {
this.contextRunner
.withUserConfiguration(EmbeddedDataSourceConfiguration.class, LegacyCallbackConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class);
FlywayCallback callbackOne = context.getBean("legacyCallbackOne", FlywayCallback.class);
FlywayCallback callbackTwo = context.getBean("legacyCallbackTwo", FlywayCallback.class);
assertThat(flyway.getCallbacks()).hasSize(2);
InOrder orderedCallbacks = inOrder(callbackOne, callbackTwo);
orderedCallbacks.verify(callbackTwo).beforeMigrate(any(Connection.class));
orderedCallbacks.verify(callbackOne).beforeMigrate(any(Connection.class));
});
}
@Test
void callbacksAndLegacyCallbacksCannotBeMixed() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class,
LegacyCallbackConfiguration.class, CallbackConfiguration.class).run((context) -> {
assertThat(context).hasFailed();
assertThat(context.getStartupFailure())
.hasMessageContaining("Found a mixture of Callback and FlywayCallback beans."
+ " One type must be used exclusively.");
});
}
@Test @Test
void configurationCustomizersAreConfiguredAndOrdered() { void configurationCustomizersAreConfiguredAndOrdered() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class, this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class,
@ -519,7 +495,7 @@ class FlywayAutoConfigurationTests {
@Bean @Bean
Flyway flyway() { Flyway flyway() {
return new Flyway(); return Flyway.configure().load();
} }
@Bean @Bean
@ -572,23 +548,6 @@ class FlywayAutoConfigurationTests {
} }
@Configuration(proxyBeanMethods = false)
static class LegacyCallbackConfiguration {
@Bean
@Order(1)
FlywayCallback legacyCallbackOne() {
return mock(FlywayCallback.class);
}
@Bean
@Order(0)
FlywayCallback legacyCallbackTwo() {
return mock(FlywayCallback.class);
}
}
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class ConfigurationCustomizerConfiguration { static class ConfigurationCustomizerConfiguration {

View File

@ -96,9 +96,10 @@ class FlywayPropertiesTests {
ignoreProperties(properties, "url", "user", "password", "enabled", "checkLocation", "createDataSource"); ignoreProperties(properties, "url", "user", "password", "enabled", "checkLocation", "createDataSource");
// High level object we can't set with properties // High level object we can't set with properties
ignoreProperties(configuration, "classLoader", "dataSource", "resolvers", "callbacks"); ignoreProperties(configuration, "classLoader", "dataSource", "resolvers", "callbacks", "javaMigrations");
// Properties we don't want to expose // Properties we don't want to expose
ignoreProperties(configuration, "resolversAsClassNames", "callbacksAsClassNames"); ignoreProperties(configuration, "resolversAsClassNames", "callbacksAsClassNames", "tablespace",
"oracleSqlplusWarn");
// Handled by the conversion service // Handled by the conversion service
ignoreProperties(configuration, "baselineVersionAsString", "encodingAsString", "locationsAsStrings", ignoreProperties(configuration, "baselineVersionAsString", "encodingAsString", "locationsAsStrings",
"targetAsString"); "targetAsString");
@ -107,8 +108,6 @@ class FlywayPropertiesTests {
ignoreProperties(properties, "initSqls"); ignoreProperties(properties, "initSqls");
// Handled as dryRunOutput // Handled as dryRunOutput
ignoreProperties(configuration, "dryRunOutputAsFile", "dryRunOutputAsFileName"); ignoreProperties(configuration, "dryRunOutputAsFile", "dryRunOutputAsFileName");
// Deprecated
ignoreProperties(configuration, "errorHandlers", "errorHandlersAsClassNames");
List<String> configurationKeys = new ArrayList<>(configuration.keySet()); List<String> configurationKeys = new ArrayList<>(configuration.keySet());
Collections.sort(configurationKeys); Collections.sort(configurationKeys);
List<String> propertiesKeys = new ArrayList<>(properties.keySet()); List<String> propertiesKeys = new ArrayList<>(properties.keySet());

View File

@ -60,7 +60,7 @@
<ehcache.version>2.10.6</ehcache.version> <ehcache.version>2.10.6</ehcache.version>
<ehcache3.version>3.8.0</ehcache3.version> <ehcache3.version>3.8.0</ehcache3.version>
<embedded-mongo.version>2.2.0</embedded-mongo.version> <embedded-mongo.version>2.2.0</embedded-mongo.version>
<flyway.version>5.2.4</flyway.version> <flyway.version>6.0.1</flyway.version>
<freemarker.version>2.3.28</freemarker.version> <freemarker.version>2.3.28</freemarker.version>
<elasticsearch.version>6.8.2</elasticsearch.version> <elasticsearch.version>6.8.2</elasticsearch.version>
<glassfish-el.version>3.0.2</glassfish-el.version> <glassfish-el.version>3.0.2</glassfish-el.version>