How to set up different databases per environment in Play 2.0?

后端 未结 5 1834
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 03:02

I\'d like my Play app to use different databases for test, local and production (production is Heroku) environments.

In application.conf I have:

<
5条回答
  •  误落风尘
    2020-12-08 03:33

    You can actually still use the Play 1.0 config value naming method, in Play 2, if you, when you load config values, check if Play.isTest, and then prefix the properties you load with 'test.'. Here's a snipped:

    def configPrefix = if (play.api.Play.isTest) "test." else ""
    
    def configStr(path: String) =
      Play.configuration.getString(configPrefix + path) getOrElse
         die(s"Config value missing: $configPrefix$path")
    
    new RelDb(
      server = configStr("pgsql.server"),
      port = configStr("pgsql.port"),
      database = configStr("pgsql.database"),
      user = ...,
      password = ...)
    

    And the related config snippet:

    pgsql.server="192.168.0.123"
    pgsql.port="5432"
    pgsql.database="prod"
    ...
    
    test.pgsql.server="192.168.0.123"
    test.pgsql.port="5432"
    test.pgsql.database="test"
    ...
    

    Now you don't need to remember setting any system properties when you run your e2e test suite, and you won't accidentally connect to the prod database.

    I suppose that you can optionally place the test. values in a separate file, which you would then include at the end of the main config file I think.

提交回复
热议问题