Skip to content

Commit

Permalink
Flyway is not instantiated by using Named annotation fix #623
Browse files Browse the repository at this point in the history
  • Loading branch information
jknack committed Feb 8, 2017
1 parent f726efc commit 819ba0a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
42 changes: 42 additions & 0 deletions coverage-report/src/test/java/org/jooby/issues/Issue623.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.jooby.issues;

import java.util.Arrays;

import org.flywaydb.core.Flyway;
import org.jooby.flyway.Flywaydb;
import org.jooby.test.ServerFeature;
import org.junit.Test;

import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigValueFactory;

public class Issue623 extends ServerFeature {

{
use(ConfigFactory.empty()
.withValue("flyway.db1.url",
ConfigValueFactory.fromAnyRef("jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1"))
.withValue("flyway.db1.locations", ConfigValueFactory
.fromAnyRef(Arrays.asList("i623/fway1")))
.withValue("flyway.db2.url",
ConfigValueFactory.fromAnyRef("jdbc:h2:mem:db2;DB_CLOSE_DELAY=-1"))
.withValue("flyway.db2.locations", ConfigValueFactory
.fromAnyRef(Arrays.asList("i623/fway2"))));

use(new Flywaydb("flyway.db1"));
use(new Flywaydb("flyway.db2"));

get("/623", req -> req.require(req.param("name").value(), Flyway.class).info().current()
.getDescription());
}

@Test
public void bootstratp2dbs() throws Exception {
request()
.get("/623?name=flyway.db1")
.expect("fway1");
request()
.get("/623?name=flyway.db2")
.expect("fway2");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
create table v002 (
ID int not null,
NAME varchar(100) not null
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
create table v010 (
ID int not null,
NAME varchar(100) not null
);
30 changes: 18 additions & 12 deletions jooby-flyway/src/main/java/org/jooby/flyway/Flywaydb.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,29 @@ public Flywaydb() {
}

@Override
public void configure(final Env env, final Config config, final Binder binder) {
Config $flyway = config.getConfig(name)
.withFallback(config.getConfig("flyway"));
public void configure(final Env env, final Config conf, final Binder binder) {
Config $flyway = conf.getConfig(name)
.withFallback(conf.getConfig("flyway"));

Flyway flyway = new Flyway();
flyway.configure(props($flyway));
commands($flyway).forEach(cmd -> cmd.run(flyway));
binder.bind(Flyway.class).toInstance(flyway);
// bind
env.serviceKey()
.generate(Flyway.class, name, key -> binder.bind(key).toInstance(flyway));
// run
Iterable<Command> cmds = commands($flyway);
env.onStart(() -> {
cmds.forEach(cmd -> cmd.run(flyway));
});
}

@Override
public Config config() {
return ConfigFactory.parseResources(getClass(), "flyway.conf");
}

@SuppressWarnings({"unchecked", "rawtypes" })
private Properties props(final Config config) {
private static Properties props(final Config config) {
Properties props = new Properties();
config.withoutPath("run").entrySet().forEach(prop -> {
Object value = prop.getValue().unwrapped();
Expand All @@ -196,13 +207,8 @@ private Properties props(final Config config) {
return props;
}

@Override
public Config config() {
return ConfigFactory.parseResources(getClass(), "flyway.conf");
}

@SuppressWarnings("unchecked")
private Iterable<Command> commands(final Config config) {
private static Iterable<Command> commands(final Config config) {
Object value = config.getAnyRef("run");
List<String> commands = new ArrayList<>();
if (value instanceof List) {
Expand Down

0 comments on commit 819ba0a

Please sign in to comment.