RoR:如何针对多个数据库测试我的应用程序? [英] RoR: how do I test my app against multiple databases?

查看:70
本文介绍了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.

这是什么?

推荐答案

@awendt友善地指出我可以回答自己的问题。

@awendt kindly pointed out that I could answer my own question.

事实证明,配方非常简单。秘诀是使用环境变量告诉Rails您要使用哪个数据库。

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.

config / database.yml 中,包括如下ERB构造:

In config/database.yml, include ERB constructs like this:

test:
<% if (ENV["RAILS_DB"] == "PostgreSQL") %>
  adapter: postgresql
  encoding: unicode
  database: bd_test
  pool: 5
  username: <%= ENV['POSTGRESQL_USERNAME'] || 'root' %>
  password: <%= ENV['POSTGRESQL_PASSWORD'] || '' %>
<% elsif (ENV["RAILS_DB"] == "MySQL") %>
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: bd_test
  pool: 5
  username: <%= ENV['MYSQL_USERNAME'] || 'root' %>
  password: <%= ENV['MYSQL_PASSWORD'] || '' %>
  socket: <%= ENV['MYSQL_SOCKET'] || '/tmp/mysql.sock' %>
<% else %>
  # default to SQLite
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000
<% end %>

注1:我仅在测试环境中显示过它。实际上,这是我唯一修改过的数据库,因为(据称)它提供了足够的覆盖范围,可以告诉我是否正确支持了所有三个数据库。

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.

注2:不需要使用环境变量来设置用户名和密码-这只是我喜欢做的事情,因为它避免了在通常查看的文件中公开密码。

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.

如下扩展Gemfile(请注意您的版本号可能会有所不同):

Similarly, extend Gemfile as follows (note that your version numbers may vary):

source 'http://rubygems.org'
gem 'rails', '3.0.3'
case ENV["RAILS_DB"]
when "PostgreSQL"
  gem 'pg', '0.10.0'
when "MySQL"
  gem 'mysql2'
else
  gem 'sqlite3', '1.3.3'
  gem 'sqlite3-ruby', '1.3.3', :require => 'sqlite3'
end
...



2。在代码中添加条件



尽管Rails开发团队尽了最大的努力,但在某些地方ActiveRecord构造并不兼容所有数据库类型。在这些情况下,您可以将代码置于 ActiveRecord :: Base.connection.adapter_name 上。这是我的一个迁移文件中的示例:

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:

文件:migration / 20110129023453_create_cached_web_pages.rb

def self.up
  create_table :cached_web_pages do |t|
    t.string    :key             
    if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
      t.binary    :value
    else
      t.binary    :value, :limit => 16777215
    end
  end
end
...



< h1> 3。运行和测试

您现在只需设置RAILS_DB环境变量即可选择数据库,但有一个陷阱:您必须运行 bundle install 每次从Gemfile设置适当的数据库适配器。幸运的是,这正是测试代码所做的。因此,例如,我可以在两个窗口中运行rspec的自动测试:

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:

$ RAILS_DB=SQLite autotest

$ RAILS_DB=PostgreSQL autotest

现在我可以破解我的文件了,如果我被破坏了,自动测试会悄悄地提醒我随便什么。

Now I can hack away at my files and autotest will quietly alert me if I've broken anything as I go along.

这篇关于RoR:如何针对多个数据库测试我的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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