Polish "Auto-configure Flyway with JavaMigration beans"

See gh-17993
pull/18077/head
Andy Wilkinson 5 years ago
parent ff68295928
commit e2edb6a539

@ -123,8 +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);
List<JavaMigration> migrations = javaMigrations.stream().collect(Collectors.toList());
configureJavaMigrations(configuration, migrations);
return configuration.load();
}
@ -218,6 +218,12 @@ public class FlywayAutoConfiguration {
}
}
private void configureJavaMigrations(FluentConfiguration flyway, List<JavaMigration> migrations) {
if (!migrations.isEmpty()) {
flyway.javaMigrations(migrations.toArray(new JavaMigration[0]));
}
}
private String getProperty(Supplier<String> property, Supplier<String> defaultValue) {
String value = property.get();
return (value != null) ? value : defaultValue.get();

@ -482,77 +482,17 @@ 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 {
}
@Configuration(proxyBeanMethods = false)
static class FlywayJavaMigrationsConfiguration {
@Bean
TestMigration migration1() {
return new TestMigration("2", "M1");
}
@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 {
}
@Bean
TestMigration migration2() {
return new TestMigration("3", "M2");
}
}
@ -669,4 +609,47 @@ class FlywayAutoConfigurationTests {
}
private static final class TestMigration implements JavaMigration {
private final MigrationVersion version;
private final String description;
private TestMigration(String version, String description) {
this.version = MigrationVersion.fromVersion(version);
this.description = description;
}
@Override
public MigrationVersion getVersion() {
return this.version;
}
@Override
public String getDescription() {
return this.description;
}
@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) {
}
}
}

@ -1907,7 +1907,7 @@ Spring Boot supports two higher-level migration tools: https://flywaydb.org/[Fly
==== Execute Flyway Database Migrations on Startup
To automatically run Flyway database migrations on startup, add the `org.flywaydb:flyway-core` to your classpath.
The migrations are scripts in the form `V<VERSION>__<NAME>.sql` (with `<VERSION>` an underscore-separated version, such as '`1`' or '`2_1`').
Typically, migrations are scripts in the form `V<VERSION>__<NAME>.sql` (with `<VERSION>` an underscore-separated version, such as '`1`' or '`2_1`').
By default, they are in a folder called `classpath:db/migration`, but you can modify that location by setting `spring.flyway.locations`.
This is a comma-separated list of one or more `classpath:` or `filesystem:` locations.
For example, the following configuration would search for scripts in both the default classpath location and the `/opt/migration` directory:
@ -1928,6 +1928,8 @@ Assume the following:
Rather than using `db/migration`, the preceding configuration sets the folder to use according to the type of the database (such as `db/migration/mysql` for MySQL).
The list of supported databases is available in {sc-spring-boot}/jdbc/DatabaseDriver.{sc-ext}[`DatabaseDriver`].
Migrations can also be written in Java. Flyway will be auto-configured with any beans that implement `JavaMigration`.
{sc-spring-boot-autoconfigure}/flyway/FlywayProperties.{sc-ext}[`FlywayProperties`] provides most of Flyway's settings and a small set of additional properties that can be used to disable the migrations or switch off the location checking.
If you need more control over the configuration, consider registering a `FlywayConfigurationCustomizer` bean.

Loading…
Cancel
Save