21

Is there any trivial way to copy the data from developmenet database into the test one? I know theres a way to copy schema and recreate database, but is there any rake task to populate test database with development one?

6 Answers 6

17

You can use mysql directly:

mysqldump app_development | mysql app_test
Sign up to request clarification or add additional context in comments.

1 Comment

if your not using mysql, @j0k has best answer so far with: rake db:test:clone && rake db:seed RAILS_ENV='test'
14

You can use:

rake db:test:clone

To copy the development db into test.

6 Comments

Doesn't work for me at all. Structure is created, but no records are copied. Is that task ment to copy the data at all? The description isn't pointing that directly.
db:test:clone is a combination of db:schema:dump and db:test:prepare and does not actually copy the data
Unfortunately doesn't answer the question.
this is deprecated.
@mdrozdziel, the test db should never have and data, the data should be created for each test.
|
6

For all databases:

rake db:test:clone && rake db:seed RAILS_ENV='test'

1 Comment

ops, i thought that you wanted to populate test database with the seed.rb file.
6

With Postgres, copy the database like so:

CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;

1 Comment

I didn't use this command but it helped create a copy of the db using pgAdmin.
4

If you just want to clone the development DB in its entirety, what's wrong with just copying the development.sqlite3 and renaming it test.sqlite3? You can automate the process by setting up a batch file (or its equivalent on your OS) that you can run from the command line.

This will work locally, but I just realized you might be thinking a non-local environment, in which case it probably won't.

Comments

4

An alternative method if you use seeds (db/seeds.rb)

First, add a rake task for example to lib/tasks/test_seed.rake with this code:

namespace :db do
  namespace :test do
    task :prepare => :environment do
        Rake::Task["db:seed"].invoke
    end
  end
end

Then whenever you changed your database structure / content through migration and seeds, you can run

rake:db:test:prepare

To copy the schema and seed data.

So the complete steps would be:

rake db:migrate
rake db:seed
rake db:test:prepare

1 Comment

If you run rake db:test:prepare after rake db:seed it will delete your seed data =]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.