Felix
Felix

Reputation: 5619

playframework database connection set default and development database

Hi in my playframework application I want to configure several database servers. I want one default for deployment. And a second one for development.

At the moment I have only one defined like this:

slick {
  dbs {
    default {
      profile = "slick.jdbc.MySQLProfile$"
      db {
        driver = "com.mysql.jdbc.Driver"
        url = ""
        user = ""
        password = 
      }
    }
  }
}

how can I add a second one ... Maybe like this:

slick {
  dbs {
    default {
      profile = "slick.jdbc.MySQLProfile$"
      db {
        driver = "com.mysql.jdbc.Driver"
        url = ""
        user = ""
        password = 
      }
    },
    development {
      profile = "slick.jdbc.MySQLProfile$"
      db {
        driver = "com.mysql.jdbc.Driver"
        url = ""
        user = ""
        password = 
      }
    }
  }
}

Is that correct? And how can I manage in which case I use the default and when the dev database?

Thanks in advance.

Upvotes: 1

Views: 94

Answers (1)

RP-
RP-

Reputation: 5837

If your application needs only one database - and you want to use one for local development and one for production (obviously), then you are only talking about one database. And the way you configure them is by using two different conf files like for local you use application.conf and for prod you use prod-env.conf and while you start the server in prod, you pass the prod-env.conf as a parameter to use.

https://www.playframework.com/documentation/2.6.x/Deploying#Using-the-dist-task

If you have multiple databases in one environment (like, if you have to deal with two different databases in your local (or prod)) - then you need to use NamedDatabase("dbName") injection as mentioned here

Update

sbt won't take -D option if you are not running it in the interactive mode. You have two options, the easiest is to run in the interactive mode

RPs-MacBook-Pro:play RP$ sbt
[info] Loading project definition from /Users/RP/play/project
[info] Resolving key references (11994 settings) ...
[info] Set current project to play (in build file:/Users/RP/play/)
[play] $

and then execute ~run "-Dconfig.file=conf/application-dev.conf", it should work just fine.

The other option is to export a system variable (ex: APP_DEV_CONF_FILE) and use it in build.sbt

val devConfigFile = sys.env.get("APP_DEV_CONF_FILE") match {
  case Some(config) => config
  case None => "application-prod"
}

javaOptions in run ++= Seq(
  "-Dconfig.file=conf/" + devConfigFile + ".conf"
)

With this, you can directly use sbt run, no need of running in interactive mode.

Upvotes: 1

Related Questions