RoR:如何针对多个数据库测试我的应用程序?
我开始在 Heroku
上部署最新的 RoR 应用程序,这要求我开始使用 PostgreSQL——我之前一直使用 SQLite 和 MySQL。我想要一种非常简单的方法来持续对所有三个数据库进行红/绿测试,以确保我在开发过程中不会破坏任何东西。
有什么好的方法可以做到这一点?
I started deploying my latest RoR app on Heroku
, which required me to start using PostgreSQL -- I'd previously been using SQLite and MySQL. I wanted a dead-simple way to continually do red/green testing against all three databases to make sure I didn't break anything in the heat of development.
What's a good way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@awendt 善意地指出我可以回答我自己的问题。
事实证明食谱相当简单。秘诀是使用环境变量来告诉 Rails 您要使用哪个数据库。
1. 修改文件
在
config/database.yml
中,包含如下 ERB 结构:注 1:我仅在测试环境中展示它。事实上,这是我修改的唯一一个,因为(据说)它提供了足够的覆盖范围来告诉我是否所有三个数据库都得到正确支持。
注 2:您不需要使用环境变量来设置用户名和密码——这正是我更喜欢做的事情,因为它可以避免在经常查看的文件中暴露密码。
同样,按如下方式扩展 Gemfile(请注意,您的版本号可能会有所不同):
2. 在代码中添加条件
尽管 Rails 开发团队尽了最大努力,但仍有一些地方 ActiveRecord 构造不兼容所有类型的数据库。在这些情况下,您可以在
ActiveRecord::Base.connection.adapter_name
上调整代码。以下是我的一个迁移文件中的示例:file: migrate/20110129023453_create_cached_web_pages.rb
3. 运行和测试
您现在可以通过设置 RAILS_DB 环境变量来选择数据库,但有一个问题:您有每次运行
bundle install
以从 Gemfile 设置适当的数据库适配器。幸运的是,这正是测试代码的作用。因此,例如,我可以在两个窗口中运行 rspec 的自动测试:现在
我可以修改我的文件,如果我在运行过程中损坏了任何东西,自动测试会悄悄地提醒我。
@awendt kindly pointed out that I could answer my own question.
It turns out the recipe is rather simple. The secret is to use a environment variable to tell Rails which db you want to use.
1. Modifying your files
In
config/database.yml
, include ERB constructs like this:Note 1: I've only shown it for the test environment. In fact, that's the only one I've modified, since (supposedly) it provides enough coverage to tell me if all three databases are properly supported.
Note 2: You don't need to use environment variables to set username and password -- that's just something I prefer to do since it avoids exposing passwords in a commonly viewed file.
Similarly, extend Gemfile as follows (note that your version numbers may vary):
2. Add conditions to your code
Despite the best efforts of the Rails development team, there are a few spots where ActiveRecord constructs aren't compatible across all flavors of database. In these cases, you can condition your code on
ActiveRecord::Base.connection.adapter_name
. Here's an example from one of my migration files:file: migrate/20110129023453_create_cached_web_pages.rb
3. Running and testing
You can now select a database simply by setting the RAILS_DB environment variable, but there's a catch: you have to run
bundle install
each time to set up the appropriate database adaptor from the Gemfile. Fortunately, that's exactly what the test code does. So, for example, I can run rspec's autotest in two windows:and
Now I can hack away at my files and autotest will quietly alert me if I've broken anything as I go along.