Skip to content

Commit 819ba0a

Browse files
committed
Flyway is not instantiated by using Named annotation fix #623
1 parent f726efc commit 819ba0a

4 files changed

Lines changed: 68 additions & 12 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.jooby.issues;
2+
3+
import java.util.Arrays;
4+
5+
import org.flywaydb.core.Flyway;
6+
import org.jooby.flyway.Flywaydb;
7+
import org.jooby.test.ServerFeature;
8+
import org.junit.Test;
9+
10+
import com.typesafe.config.ConfigFactory;
11+
import com.typesafe.config.ConfigValueFactory;
12+
13+
public class Issue623 extends ServerFeature {
14+
15+
{
16+
use(ConfigFactory.empty()
17+
.withValue("flyway.db1.url",
18+
ConfigValueFactory.fromAnyRef("jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1"))
19+
.withValue("flyway.db1.locations", ConfigValueFactory
20+
.fromAnyRef(Arrays.asList("i623/fway1")))
21+
.withValue("flyway.db2.url",
22+
ConfigValueFactory.fromAnyRef("jdbc:h2:mem:db2;DB_CLOSE_DELAY=-1"))
23+
.withValue("flyway.db2.locations", ConfigValueFactory
24+
.fromAnyRef(Arrays.asList("i623/fway2"))));
25+
26+
use(new Flywaydb("flyway.db1"));
27+
use(new Flywaydb("flyway.db2"));
28+
29+
get("/623", req -> req.require(req.param("name").value(), Flyway.class).info().current()
30+
.getDescription());
31+
}
32+
33+
@Test
34+
public void bootstratp2dbs() throws Exception {
35+
request()
36+
.get("/623?name=flyway.db1")
37+
.expect("fway1");
38+
request()
39+
.get("/623?name=flyway.db2")
40+
.expect("fway2");
41+
}
42+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
create table v002 (
2+
ID int not null,
3+
NAME varchar(100) not null
4+
);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
create table v010 (
2+
ID int not null,
3+
NAME varchar(100) not null
4+
);

jooby-flyway/src/main/java/org/jooby/flyway/Flywaydb.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,29 @@ public Flywaydb() {
173173
}
174174

175175
@Override
176-
public void configure(final Env env, final Config config, final Binder binder) {
177-
Config $flyway = config.getConfig(name)
178-
.withFallback(config.getConfig("flyway"));
176+
public void configure(final Env env, final Config conf, final Binder binder) {
177+
Config $flyway = conf.getConfig(name)
178+
.withFallback(conf.getConfig("flyway"));
179179

180180
Flyway flyway = new Flyway();
181181
flyway.configure(props($flyway));
182-
commands($flyway).forEach(cmd -> cmd.run(flyway));
183-
binder.bind(Flyway.class).toInstance(flyway);
182+
// bind
183+
env.serviceKey()
184+
.generate(Flyway.class, name, key -> binder.bind(key).toInstance(flyway));
185+
// run
186+
Iterable<Command> cmds = commands($flyway);
187+
env.onStart(() -> {
188+
cmds.forEach(cmd -> cmd.run(flyway));
189+
});
190+
}
191+
192+
@Override
193+
public Config config() {
194+
return ConfigFactory.parseResources(getClass(), "flyway.conf");
184195
}
185196

186197
@SuppressWarnings({"unchecked", "rawtypes" })
187-
private Properties props(final Config config) {
198+
private static Properties props(final Config config) {
188199
Properties props = new Properties();
189200
config.withoutPath("run").entrySet().forEach(prop -> {
190201
Object value = prop.getValue().unwrapped();
@@ -196,13 +207,8 @@ private Properties props(final Config config) {
196207
return props;
197208
}
198209

199-
@Override
200-
public Config config() {
201-
return ConfigFactory.parseResources(getClass(), "flyway.conf");
202-
}
203-
204210
@SuppressWarnings("unchecked")
205-
private Iterable<Command> commands(final Config config) {
211+
private static Iterable<Command> commands(final Config config) {
206212
Object value = config.getAnyRef("run");
207213
List<String> commands = new ArrayList<>();
208214
if (value instanceof List) {

0 commit comments

Comments
 (0)