Auto-configure Flyway with JavaMigration beans
See gh-17993
This commit is contained in:
committed by
Andy Wilkinson
parent
82ea3b51b3
commit
ff68295928
@@ -31,6 +31,7 @@ import org.flywaydb.core.Flyway;
|
||||
import org.flywaydb.core.api.MigrationVersion;
|
||||
import org.flywaydb.core.api.callback.Callback;
|
||||
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;
|
||||
@@ -112,7 +113,7 @@ public class FlywayAutoConfiguration {
|
||||
ResourceLoader resourceLoader, ObjectProvider<DataSource> dataSource,
|
||||
@FlywayDataSource ObjectProvider<DataSource> flywayDataSource,
|
||||
ObjectProvider<FlywayConfigurationCustomizer> fluentConfigurationCustomizers,
|
||||
ObjectProvider<Callback> callbacks) {
|
||||
ObjectProvider<JavaMigration> javaMigrations, ObjectProvider<Callback> callbacks) {
|
||||
FluentConfiguration configuration = new FluentConfiguration(resourceLoader.getClassLoader());
|
||||
DataSource dataSourceToMigrate = configureDataSource(configuration, properties, dataSourceProperties,
|
||||
flywayDataSource.getIfAvailable(), dataSource.getIfAvailable());
|
||||
@@ -122,6 +123,8 @@ public class FlywayAutoConfiguration {
|
||||
configureCallbacks(configuration, orderedCallbacks);
|
||||
fluentConfigurationCustomizers.orderedStream().forEach((customizer) -> customizer.customize(configuration));
|
||||
configureFlywayCallbacks(configuration, orderedCallbacks);
|
||||
JavaMigration[] migrations = javaMigrations.stream().toArray(JavaMigration[]::new);
|
||||
configuration.javaMigrations(migrations);
|
||||
return configuration.load();
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.flywaydb.core.api.MigrationVersion;
|
||||
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.migration.JavaMigration;
|
||||
import org.flywaydb.core.internal.license.FlywayProUpgradeRequiredException;
|
||||
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -264,6 +265,16 @@ class FlywayAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void flywayJavaMigrations() {
|
||||
this.contextRunner
|
||||
.withUserConfiguration(EmbeddedDataSourceConfiguration.class, FlywayJavaMigrationsConfiguration.class)
|
||||
.run((context) -> {
|
||||
Flyway flyway = context.getBean(Flyway.class);
|
||||
assertThat(flyway.getConfiguration().getJavaMigrations().length).isEqualTo(2);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void customFlywayMigrationInitializer() {
|
||||
this.contextRunner
|
||||
@@ -471,6 +482,81 @@ class FlywayAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class FlywayJavaMigrationsConfiguration {
|
||||
|
||||
@Component
|
||||
private static class Migration1 implements JavaMigration {
|
||||
|
||||
@Override
|
||||
public MigrationVersion getVersion() {
|
||||
return MigrationVersion.fromVersion("2");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "M1";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getChecksum() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUndo() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExecuteInTransaction() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void migrate(org.flywaydb.core.api.migration.Context context) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Component
|
||||
private static class Migration2 implements JavaMigration {
|
||||
|
||||
@Override
|
||||
public MigrationVersion getVersion() {
|
||||
return MigrationVersion.fromVersion("3");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "M2";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getChecksum() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUndo() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExecuteInTransaction() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void migrate(org.flywaydb.core.api.migration.Context context) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class ResourceLoaderConfiguration {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user