Majiy
Majiy

Reputation: 1920

How can I copy my production-database to my test-database in Ruby on Rails?

I have a well-running production environment. For purposes of testing, I want to copy all database tables (including table content!) to my test environment.

I tried rake db:test:clone, but this only creates the table structures, not the contents.

Is there a rake task or something comparable for this?

Upvotes: 2

Views: 3904

Answers (5)

Eric
Eric

Reputation: 3792

You could use the yml_db gem. Just do the following:

  1. Include the following in your gemfile and run bundle install

gem 'yaml_db'

  1. Create a dump file of your current db

bundle exec rake db:data:dump RAILS_ENV=production

*Note you need to do this from a console that can access your production database using the settings in database.yml (or wherever you have them). I was able to set up a ssh tunnel to my prod server and then just run the command locally but you may need to run it from the server and then download the /db/data.yml file.

  1. Assuming your dump file is now in /db/data.yml and you have an empty migrated db you can run the following

bundle exec rake db:data:dump RAILS_ENV=development

If your db isn't empty and migrated just run rake reset first.

Read this post for more info.

Upvotes: 0

sailor
sailor

Reputation: 8034

You can use taps gem as showed here: http://railscasts.com/episodes/342-migrating-to-postgresql

Upvotes: 0

pjammer
pjammer

Reputation: 9577

I don't think there is something that you want, but I'm not sure why you'd want that knowing how test environments work, that'd you want it to happen. You basically want a shell command to restore from a dump.

In the testing environment, your database is broken down and rebuilt "basically" after each test is ran. It will load up your fixtures/factories and then apply the test that it runs. The thought with the testing environment is that you test everything in isolation, unless they are integration tests which have a sense of what 'some other test' did previously.

Now all that to say, if your production data is large, it may suck to build that big production data each time.

If the data is essential, copy it into a fixture or factory and you'll have the same data repeatable.

Is there a smaller reason why you want the whole DB and not just one or two pieces of the data?

Upvotes: 0

Said Kaldybaev
Said Kaldybaev

Reputation: 9952

Try this one:

rake db:test:prepare

Upvotes: 1

Related Questions