Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-configure Flyway with any JavaMigration beans #17993

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Map;

import org.flywaydb.core.api.configuration.ClassicConfiguration;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.flyway.FlywayEndpoint.FlywayDescriptor;
Expand Down Expand Up @@ -56,7 +57,7 @@ void flywayReportIsProduced() {
@SuppressWarnings("deprecation")
void whenFlywayHasBeenBaselinedFlywayReportIsProduced() {
this.contextRunner.withBean(FlywayMigrationStrategy.class, () -> (flyway) -> {
flyway.setBaselineVersionAsString("2");
((ClassicConfiguration) flyway.getConfiguration()).setBaselineVersionAsString("2");
flyway.baseline();
flyway.migrate();
}).run((context) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
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.flywaydb.core.api.migration.JavaMigration;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
Expand Down Expand Up @@ -81,6 +81,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 +114,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<JavaMigration> javaMigrations, ObjectProvider<Callback> callbacks) {
FluentConfiguration configuration = new FluentConfiguration(resourceLoader.getClassLoader());
DataSource dataSourceToMigrate = configureDataSource(configuration, properties, dataSourceProperties,
flywayDataSource.getIfAvailable(), dataSource.getIfAvailable());
Expand All @@ -122,10 +123,10 @@ 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);
JavaMigration[] migrations = javaMigrations.stream().toArray(JavaMigration[]::new);
configuration.javaMigrations(migrations);
return configuration.load();
}

private DataSource configureDataSource(FluentConfiguration configuration, FlywayProperties properties,
Expand Down Expand Up @@ -210,14 +211,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
Loading