Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 221
William Jones Avatar asked Apr 12 '10 21:04

William Jones


2 Answers

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

like image 101
Johnsonch Avatar answered Oct 27 '22 00:10

Johnsonch


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
like image 29
Hakan Ensari Avatar answered Oct 26 '22 22:10

Hakan Ensari