开发者

Ruby on Rails: Accessing production database data for testing

With Ruby on Rails, is there a way for me to dump my production database into a form that the test part of Rails can access?

I'm thinking either a way to turn the production database into fi开发者_运维百科xtures, or else a way to migrate data from the production database into the test database that will not get routinely cleared out by Rails.

I'd like to use this data for a variety of tests, but foremost in my mind is using real data with the performance tests, so that I can get a realistic understanding of load times.


You can also check out http://github.com/napcs/lazy_developer which will allow you to put the production data into yaml files.


We just had a similar problem and ended up writing a helper method in rspec that fetches some data (in our case, login details for some accounts) from the production database.

The following should give an idea:

require 'yaml'

def accounts
  @accounts ||= lambda {
    config = YAML.load_file("#{Rails.root}/config/database.yml")['production']

    dbh = Mysql.real_connect(config['host'], config['username'], config['password'], config['database'])

    accounts = []
    result = dbh.query("SELECT `accounts`.* FROM `accounts`")
    while row = result.fetch_hash do
      row.delete("id")
      accounts << Account.make(row)
    end

    dbh.close

    return accounts
  }.call
end


You can use the seed.rb inside the db folder, populate your test database with the data you need. There is nice example available on Railscasts: http://railscasts.com/episodes?search=seed

Would recommend you though to keep your production data away from your testing environments. And do make backups!!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜