How to Apply an Existing Evolution Scripts to a Database that will be defined at Runtime?

configuration
slick

(Underscore05) #1

Hello,

Any help would. Here is my use-case.

I have an evolution scripts stored at conf/evolutions/templatedb and during runtime, I want to create an instance of play.api.db.Database given a dynamic values that I will get from the user (right now I don’t know how to). And apply the evolution scripts to that Database.
Something like this one.

val url = "jdbc:postgresql://localhost:5432/somedatabase"
val db = Database(url....)
Evolutions.applyEvolutions(db, "templatedb")

(Greg Methvin) #2

I think play.api.db.Databases has what you want.


(Underscore05) #3

Already got the answer Greg. Thanks. Btw, here’s what I’ve done.

val username = "..."
val password = "...."
val url = "jdbc:postgresql://localhost:5432/somedatabase"
Databases.withDatabase(
      url = url,
      driver = "org.postgresql.Driver",
      config = Map(
        "username" ->  username,
        "password" -> password
      ),
      name = "templatedb"
    )(Evolutions.applyEvolutions(_))

Though this requires me to remove slick-evolutions from the build.sbt and add evolutions instead.