Rails - Populate test database with development da

2019-03-09 03:18发布

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条回答
一夜七次
2楼-- · 2019-03-09 03:51

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
查看更多
甜甜的少女心
3楼-- · 2019-03-09 03:53

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.

查看更多
淡お忘
4楼-- · 2019-03-09 03:59

With Postgres, copy the database like so:

CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;

查看更多
何必那么认真
5楼-- · 2019-03-09 04:05

You can use mysql directly:

mysqldump app_development | mysql app_test
查看更多
劫难
6楼-- · 2019-03-09 04:05

You can use:

rake db:test:clone

To copy the development db into test.

查看更多
\"骚年 ilove
7楼-- · 2019-03-09 04:15

For all databases:

rake db:test:clone && rake db:seed RAILS_ENV='test'
查看更多
登录 后发表回答