如何在Play 2.0中为每个环境设置不同的数据库?

编程入门 行业动态 更新时间:2024-10-21 17:27:29
本文介绍了如何在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.default.url]](Configuration.scala:258)

我有几个问题:

  • 一般来说,我对在Play中如何配置数据库有一点困惑:在Play中:它看起来像是一个简单的 db , db。 [DBNAME] 和 db。 [DBNAME] .url ,不同的教程在中做出不同的选择。某些看起来像他们应该工作的表达式(例如 db.default.url =jdbc:...失败,并显示一个错误, )

  • 我看过其他人建议我分别创建 prod.conf c $ c> 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示例,请参阅: github/jamesward/play2bars/blob/scala-anorm/Procfile

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

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?

解决方案

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: github/jamesward/play2bars/blob/scala-anorm/Procfile

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

更多推荐

如何在Play 2.0中为每个环境设置不同的数据库?

本文发布于:2023-07-14 21:55:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107259.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中为   环境   数据库   如何在   Play

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!