I'm using Slick 3.1.1 and Play 2.5.10 and have the following application.conf
:
slick.dbs {
default {
driver="slick.driver.PostgresDriver$"
db.driver="org.postgresql.Driver"
db.url="jdbc:postgresql://localhost:5432/exampledb?searchpath=public"
db.user="postgres"
db.password="postgres"
}
test {
driver="slick.driver.H2Driver$"
db.driver="org.h2.Driver"
db.url="jdbc:h2:mem:test;MODE=PostgreSQL"
db.username="sa"
db.password=""
}
}
and to test my Daos I'd like to create a test database using evolutions so I do:
package dao
import org.scalatest.BeforeAndAfter
import play.api.db.evolutions.Evolutions
trait BeforeAndAfterDao extends BeforeAndAfter {
before {
Evolutions.applyEvolutions("test") // <<< doesn't compile, needs Database instance
}
after {
Evolutions.cleanupEvolutions("test") // <<< doesn't compile, needs Database instance
}
}
but unfortunately I need to create a play.api.db.Database
to pass to the apply/cleanupEvolutions
. How can I build the required Database instance from the application.conf
?
Well, you can just do it manually in two steps.
First step:
I am assuming that you have your Config
somewhere already so you can just inject it like this:
@Inject()(config: Config)
if you don't (which you normally always have as most of the times you need to read something from it anyway) you can always expose it as follows (inside your injectable Module
):
bind[Config].to(ConfigFactory.load("application.conf")).in(classOf[Singleton])
and then:
val driver = config.getString("slick.dbs.default.db.driver")
val url = config.getString("slick.dbs.default.db.url")
val user = config.getString("slick.dbs.default.db.user")
val password = config.getString("slick.dbs.default.db.password")
Second create your database:
val db = Databases(driver, url,
config = Map("username" -> user,
"password" -> password))