Skip to content

Commit

Permalink
Upgrade to Flyway 6.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
SammyVimes authored and wilkinsona committed Aug 30, 2019
1 parent 1e2f895 commit 278b20d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationVersion;
import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.callback.FlywayCallback;
import org.flywaydb.core.api.configuration.FluentConfiguration;

import org.springframework.beans.factory.ObjectProvider;
Expand Down Expand Up @@ -81,6 +80,7 @@
* @author Dominic Gunn
* @author Dan Zheng
* @author András Deák
* @author Semyon Danilov
* @since 1.1.0
*/
@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -113,7 +113,7 @@ public Flyway flyway(FlywayProperties properties, DataSourceProperties dataSourc
ResourceLoader resourceLoader, ObjectProvider<DataSource> dataSource,
@FlywayDataSource ObjectProvider<DataSource> flywayDataSource,
ObjectProvider<FlywayConfigurationCustomizer> fluentConfigurationCustomizers,
ObjectProvider<Callback> callbacks, ObjectProvider<FlywayCallback> flywayCallbacks) {
ObjectProvider<Callback> callbacks) {
FluentConfiguration configuration = new FluentConfiguration(resourceLoader.getClassLoader());
DataSource dataSourceToMigrate = configureDataSource(configuration, properties, dataSourceProperties,
flywayDataSource.getIfAvailable(), dataSource.getIfAvailable());
Expand All @@ -122,10 +122,8 @@ public Flyway flyway(FlywayProperties properties, DataSourceProperties dataSourc
List<Callback> orderedCallbacks = callbacks.orderedStream().collect(Collectors.toList());
configureCallbacks(configuration, orderedCallbacks);
fluentConfigurationCustomizers.orderedStream().forEach((customizer) -> customizer.customize(configuration));
Flyway flyway = configuration.load();
List<FlywayCallback> orderedFlywayCallbacks = flywayCallbacks.orderedStream().collect(Collectors.toList());
configureFlywayCallbacks(flyway, orderedCallbacks, orderedFlywayCallbacks);
return flyway;
configureFlywayCallbacks(configuration, orderedCallbacks);
return configuration.load();
}

private DataSource configureDataSource(FluentConfiguration configuration, FlywayProperties properties,
Expand Down Expand Up @@ -210,14 +208,9 @@ private void configureCallbacks(FluentConfiguration configuration, List<Callback
}
}

private void configureFlywayCallbacks(Flyway flyway, List<Callback> callbacks,
List<FlywayCallback> flywayCallbacks) {
if (!flywayCallbacks.isEmpty()) {
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]));
private void configureFlywayCallbacks(FluentConfiguration flyway, List<Callback> callbacks) {
if (!callbacks.isEmpty()) {
flyway.callbacks(callbacks.toArray(new Callback[0]));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.boot.autoconfigure.flyway;

import java.sql.Connection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -30,7 +29,6 @@
import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.callback.Context;
import org.flywaydb.core.api.callback.Event;
import org.flywaydb.core.api.callback.FlywayCallback;
import org.flywaydb.core.internal.license.FlywayProUpgradeRequiredException;
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -89,7 +87,7 @@ void createsDataSourceWithNoDataSourceBeanAndFlywayUrl() {
this.contextRunner.withPropertyValues("spring.flyway.url:jdbc:hsqldb:mem:" + UUID.randomUUID())
.run((context) -> {
assertThat(context).hasSingleBean(Flyway.class);
assertThat(context.getBean(Flyway.class).getDataSource()).isNotNull();
assertThat(context.getBean(Flyway.class).getConfiguration().getDataSource()).isNotNull();
});
}

Expand All @@ -98,7 +96,7 @@ void createDataSourceWithUrl() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.flyway.url:jdbc:hsqldb:mem:flywaytest").run((context) -> {
assertThat(context).hasSingleBean(Flyway.class);
assertThat(context.getBean(Flyway.class).getDataSource()).isNotNull();
assertThat(context.getBean(Flyway.class).getConfiguration().getDataSource()).isNotNull();
});
}

Expand All @@ -109,7 +107,7 @@ void createDataSourceWithUser() {
"spring.flyway.user:sa")
.run((context) -> {
assertThat(context).hasSingleBean(Flyway.class);
assertThat(context.getBean(Flyway.class).getDataSource()).isNotNull();
assertThat(context.getBean(Flyway.class).getConfiguration().getDataSource()).isNotNull();
});
}

Expand All @@ -118,7 +116,7 @@ void createDataSourceFallbackToEmbeddedProperties() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.flyway.url:jdbc:hsqldb:mem:flywaytest").run((context) -> {
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).hasFieldOrPropertyWithValue("user", "sa");
assertThat(dataSource).hasFieldOrPropertyWithValue("password", "");
Expand All @@ -130,7 +128,7 @@ void createDataSourceWithUserAndFallbackToEmbeddedProperties() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.flyway.user:sa").run((context) -> {
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).extracting("url").asString().startsWith("jdbc:h2:mem:");
});
Expand All @@ -142,7 +140,7 @@ void flywayDataSource() {
.withUserConfiguration(FlywayDataSourceConfiguration.class, EmbeddedDataSourceConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(Flyway.class);
assertThat(context.getBean(Flyway.class).getDataSource())
assertThat(context.getBean(Flyway.class).getConfiguration().getDataSource())
.isEqualTo(context.getBean("flywayDataSource"));
});
}
Expand All @@ -151,7 +149,8 @@ void flywayDataSource() {
void flywayDataSourceWithoutDataSourceAutoConfiguration() {
this.contextRunner.withUserConfiguration(FlywayDataSourceConfiguration.class).run((context) -> {
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"));
});
}

Expand All @@ -175,7 +174,8 @@ void defaultFlyway() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(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"));
});
}

Expand All @@ -186,8 +186,8 @@ void overrideLocations() {
.run((context) -> {
assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class);
assertThat(flyway.getLocations()).containsExactly(new Location("classpath:db/changelog"),
new Location("classpath:db/migration"));
assertThat(flyway.getConfiguration().getLocations()).containsExactly(
new Location("classpath:db/changelog"), new Location("classpath:db/migration"));
});
}

Expand All @@ -199,8 +199,8 @@ void overrideLocationsList() {
.run((context) -> {
assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class);
assertThat(flyway.getLocations()).containsExactly(new Location("classpath:db/changelog"),
new Location("classpath:db/migration"));
assertThat(flyway.getConfiguration().getLocations()).containsExactly(
new Location("classpath:db/changelog"), new Location("classpath:db/migration"));
});
}

Expand All @@ -210,7 +210,7 @@ void overrideSchemas() {
.withPropertyValues("spring.flyway.schemas:public").run((context) -> {
assertThat(context).hasSingleBean(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]");
});
}

Expand Down Expand Up @@ -289,7 +289,8 @@ void overrideBaselineVersionString() {
.withPropertyValues("spring.flyway.baseline-version=0").run((context) -> {
assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class);
assertThat(flyway.getBaselineVersion()).isEqualTo(MigrationVersion.fromVersion("0"));
assertThat(flyway.getConfiguration().getBaselineVersion())
.isEqualTo(MigrationVersion.fromVersion("0"));
});
}

Expand All @@ -299,7 +300,8 @@ void overrideBaselineVersionNumber() {
.withPropertyValues("spring.flyway.baseline-version=1").run((context) -> {
assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class);
assertThat(flyway.getBaselineVersion()).isEqualTo(MigrationVersion.fromVersion("1"));
assertThat(flyway.getConfiguration().getBaselineVersion())
.isEqualTo(MigrationVersion.fromVersion("1"));
});
}

Expand All @@ -310,8 +312,8 @@ void useVendorDirectory() {
.run((context) -> {
assertThat(context).hasSingleBean(Flyway.class);
Flyway flyway = context.getBean(Flyway.class);
assertThat(flyway.getLocations()).containsExactlyInAnyOrder(new Location("classpath:db/vendors/h2"),
new Location("classpath:db/changelog"));
assertThat(flyway.getConfiguration().getLocations()).containsExactlyInAnyOrder(
new Location("classpath:db/vendors/h2"), new Location("classpath:db/changelog"));
});
}

Expand All @@ -321,7 +323,8 @@ void useOneLocationWithVendorDirectory() {
.withPropertyValues("spring.flyway.locations=classpath:db/vendors/{vendor}").run((context) -> {
assertThat(context).hasSingleBean(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"));
});
}

Expand All @@ -333,41 +336,14 @@ void callbacksAreConfiguredAndOrdered() {
Flyway flyway = context.getBean(Flyway.class);
Callback callbackOne = context.getBean("callbackOne", Callback.class);
Callback callbackTwo = context.getBean("callbackTwo", Callback.class);
assertThat(flyway.getCallbacks()).hasSize(2);
assertThat(flyway.getCallbacks()).containsExactly(callbackTwo, callbackOne);
assertThat(flyway.getConfiguration().getCallbacks()).hasSize(2);
assertThat(flyway.getConfiguration().getCallbacks()).containsExactly(callbackTwo, callbackOne);
InOrder orderedCallbacks = inOrder(callbackOne, callbackTwo);
orderedCallbacks.verify(callbackTwo).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
void configurationCustomizersAreConfiguredAndOrdered() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class,
Expand Down Expand Up @@ -519,7 +495,7 @@ protected CustomFlywayWithJpaConfiguration(DataSource dataSource) {

@Bean
Flyway flyway() {
return new Flyway();
return Flyway.configure().load();
}

@Bean
Expand Down Expand Up @@ -572,23 +548,6 @@ private Callback mockCallback() {

}

@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)
static class ConfigurationCustomizerConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ void expectedPropertiesAreManaged() {
ignoreProperties(properties, "url", "user", "password", "enabled", "checkLocation", "createDataSource");

// 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
ignoreProperties(configuration, "resolversAsClassNames", "callbacksAsClassNames");
ignoreProperties(configuration, "resolversAsClassNames", "callbacksAsClassNames", "tablespace",
"oracleSqlplusWarn");
// Handled by the conversion service
ignoreProperties(configuration, "baselineVersionAsString", "encodingAsString", "locationsAsStrings",
"targetAsString");
Expand All @@ -107,8 +108,6 @@ void expectedPropertiesAreManaged() {
ignoreProperties(properties, "initSqls");
// Handled as dryRunOutput
ignoreProperties(configuration, "dryRunOutputAsFile", "dryRunOutputAsFileName");
// Deprecated
ignoreProperties(configuration, "errorHandlers", "errorHandlersAsClassNames");
List<String> configurationKeys = new ArrayList<>(configuration.keySet());
Collections.sort(configurationKeys);
List<String> propertiesKeys = new ArrayList<>(properties.keySet());
Expand Down
2 changes: 1 addition & 1 deletion spring-boot-project/spring-boot-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<ehcache.version>2.10.6</ehcache.version>
<ehcache3.version>3.8.0</ehcache3.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>
<elasticsearch.version>6.8.2</elasticsearch.version>
<glassfish-el.version>3.0.2</glassfish-el.version>
Expand Down

0 comments on commit 278b20d

Please sign in to comment.