如何在Play 2.0中为每个环境设置不同的数据库? [英] How to set up different databases per environment in Play 2.0?
问题描述
我想让我的Play应用程式使用不同的资料库进行测试,本地和制作(生产环境为Heroku)环境。
在 .conf
我有:
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}
t似乎工作。当我运行玩测试
或玩跑
,
所有DB访问失败, b
$ b
配置错误[缺少配置[db.default.url]](Configuration.scala:258)
我有几个问题:
-
一般来说,我对于如何配置数据库
在Play中有点困惑:它看起来像有简单的db
,db。 [DBNAME]
和db。
,不同的教程在
[DBNAME] .url
中做出不同的选择。某些看起来像他们应该工作的表达式(例如db.default.url =jdbc:...
失败,并显示一个错误, )。 -
我看过其他人建议我分别创建
prod.conf
,dev.conf
和test.conf
文件,每个文件包含application.conf
然后包含DB特定的配置。但在这种情况下,当我从播放控制台运行test
时,如何指定要使用的数据库? -
%env
语法应该在Play 2中使用吗? -
为
玩测试指定环境的正确方法是什么?
在Play 2中没有不同的配置环境。而是您只需在 conf / application.conf
文件中设置或覆盖配置参数。一种方法是在 play
命令行,例如:
play -Ddb.default.driver = org.postgresql.Driver -Ddb.default.url = $ DATABASE_URL〜run
您还可以告诉Play使用不同的配置文件:
播放-Dconfig.file = conf / prod。 conf〜run
有关Heroku的Procfile示例,请参阅:
https://github.com/jamesward/play2bars/blob/scala-anorm/Procfile a>
Play文件中的更多详细信息:
http://www.playframework.org/documentation/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]
anddb. [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
andtest.conf
files that each includeapplication.conf
and then contain DB-specific configuration. But in that case, how do I specify what database to use when I runtest
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?
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
这篇关于如何在Play 2.0中为每个环境设置不同的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!