Skip to content

Commit 01dfdb5

Browse files
committed
flyway: manually add migration
The manually added Java-based migrations. These are not Java-based migrations discovered through classpath scanning and instantiated by Flyway. Instead, these are manually added instances of JavaMigration. This is particularly useful when working with a dependencies, where you may want to instantiate the class and wire up its dependencies. ```java { install(new FlywaydbModule() .javaMigrations(new MyMigration(new MyService()) ); } ``` fix #3796
1 parent 8cf0102 commit 01dfdb5

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

modules/jooby-flyway/src/main/java/io/jooby/flyway/FlywayModule.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
*/
66
package io.jooby.flyway;
77

8-
import java.util.HashMap;
9-
import java.util.Map;
10-
import java.util.Optional;
8+
import java.util.*;
119

1210
import javax.sql.DataSource;
1311

1412
import org.flywaydb.core.Flyway;
1513
import org.flywaydb.core.api.configuration.FluentConfiguration;
14+
import org.flywaydb.core.api.migration.JavaMigration;
1615

1716
import edu.umd.cs.findbugs.annotations.NonNull;
1817
import io.jooby.Extension;
@@ -48,6 +47,7 @@
4847
public class FlywayModule implements Extension {
4948

5049
private final String name;
50+
private List<JavaMigration> javaMigrations = List.of();
5151

5252
/**
5353
* Creates a new Flyway module.
@@ -66,16 +66,30 @@ public FlywayModule() {
6666
this("db");
6767
}
6868

69+
/**
70+
* The manually added Java-based migrations. These are not Java-based migrations discovered
71+
* through classpath scanning and instantiated by Flyway. Instead, these are manually added
72+
* instances of JavaMigration. This is particularly useful when working with a dependencies, where
73+
* you may want to instantiate the class and wire up its dependencies.
74+
*
75+
* @param migrations The manually added Java-based migrations. An empty array if none.
76+
* @return This module.
77+
*/
78+
public FlywayModule javaMigrations(@NonNull JavaMigration... migrations) {
79+
this.javaMigrations = List.of(migrations);
80+
return this;
81+
}
82+
6983
@Override
7084
public void install(@NonNull Jooby application) throws Exception {
7185
var environment = application.getEnvironment();
7286
var registry = application.getServices();
7387
var dataSource = registry.getOrNull(ServiceKey.key(DataSource.class, name));
7488
if (dataSource == null) {
75-
// TODO: replace with usage exception
7689
dataSource = registry.require(DataSource.class);
7790
}
7891
var configuration = new FluentConfiguration(environment.getClassLoader());
92+
configuration.javaMigrations(javaMigrations.toArray(new JavaMigration[0]));
7993

8094
var defaults = environment.getProperties("flyway");
8195
var overrides = environment.getProperties(name + ".flyway", "flyway");

0 commit comments

Comments
 (0)