Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 views
in Technique[技术] by (71.8m points)

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

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

In application.conf I have:

db.default.driver=org.postgresql.Driver 

%dev.db.default.url="jdbc:postgresql://localhost/foobar" 
%test.db.default.url="jdbc:postgresql://localhost/foobar-test" 
%prod.db.default.url=${DATABASE_URL} 

This doesn't seem to work. When I run play test or play run, all DB access fails with:

 Configuration error [Missing configuration [db.default.url]] (Configuration.scala:258) 

I have a few questions about this:

  • In general, I'm a little confused about how databases are configured in Play: it looks like there's plain db, db.[DBNAME] and db. [DBNAME].url and different tutorials make different choices among those. Certain expressions that seem like they should work (e.g. db.default.url = "jdbc:..." fail with an error that a string was provided where an object was expected).

  • I've seen other people suggest that I create separate prod.conf, dev.conf and test.conf files that each include application.conf and then contain DB-specific configuration. But in that case, how do I specify what database to use when I run test from the Play console?

  • Is the %env syntax supposed to work in Play 2?

  • What's the correct way to specify an environment for play test to use?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Play 2 there aren't different config environments. Instead you just set or override the config parameters in the conf/application.conf file. One way to do it is on the play command line, like:

play -Ddb.default.driver=org.postgresql.Driver -Ddb.default.url=$DATABASE_URL ~run

You can also tell Play to use a different config file:

play -Dconfig.file=conf/prod.conf ~run

For an example Procfile for Heroku, see:
https://github.com/jamesward/play2bars/blob/scala-anorm/Procfile

More details in the Play Docs:
http://www.playframework.org/documentation/2.0/Configuration


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...