Skip to content

Commit

Permalink
Polish "Auto-configure Flyway with JavaMigration beans"
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona authored and pull[bot] committed Aug 30, 2019
1 parent 3abc842 commit f004338
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public Flyway flyway(FlywayProperties properties, DataSourceProperties dataSourc
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();
}

Expand Down Expand Up @@ -218,6 +218,12 @@ private void configureFlywayCallbacks(FluentConfiguration flyway, List<Callback>
}
}

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,77 +482,17 @@ DataSource flywayDataSource() {

}

@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");
}

}
Expand Down Expand Up @@ -669,4 +609,47 @@ private CustomClassLoader(ClassLoader parent) {

}

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) {

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.

Expand Down

0 comments on commit f004338

Please sign in to comment.