views:

213

answers:

3

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 fixtures, 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.

A: 

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!!!

Shyam
Well, I'm envisioning that the production => testing sharing of data will be a strictly one way process.Regarding db:seed, thanks for the tip, I'd never heard of it before. However, I'm not really seeing a way to use this to seed my test database with the information from my production database?
WIlliam Jones
+2  A: 

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

Johnsonch
+1  A: 

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
hakanensari