如何在 Play 2.0 中为每个环境设置不同的数据库? [英] How to set up different databases per environment in Play 2.0?

查看:16
本文介绍了如何在 Play 2.0 中为每个环境设置不同的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 Play 应用对测试、本地和生产(生产是 Heroku)环境使用不同的数据库.

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

application.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} 

这似乎不起作用.当我运行 play testplay run 时,所有数据库访问都失败:

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) 

我对此有几个问题:

  • 总的来说,我对数据库的配置方式有点困惑在 Play 中:看起来有普通的 dbdb.[DBNAME]db.[DBNAME].url 和不同的教程在其中做出不同的选择那些.某些看起来应该可以工作的表达式(例如 db.default.url = "jdbc:..." 失败,错误是在期望对象的地方提供了字符串).

  • 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).

我看到其他人建议我创建单独的 prod.confdev.conftest.conf 文件每个都包含 application.conf 然后包含特定于数据库的配置.但在这种情况下,当我从 Play 控制台运行 test 时,如何指定要使用的数据库?

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?

%env 语法是否应该在 Play 2 中工作?

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

play test指定环境的正确方法是什么?

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

推荐答案

在 Play 2 中没有不同的配置环境.相反,您只需设置或覆盖 conf/application.conf 文件中的配置参数.一种方法是在 play 命令行上,例如:

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

您还可以告诉 Play 使用不同的配置文件:

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

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

有关 Heroku 的示例 Procfile,请参阅:
https://github.com/jamesward/play2bars/blob/scala-anorm/Procfile

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

Play 文档中的更多详细信息:
http://www.playframework.org/documentation/2.0/Configuration

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

这篇关于如何在 Play 2.0 中为每个环境设置不同的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆