Rails - Populate test database with development data

后端 未结 6 1150
没有蜡笔的小新
没有蜡笔的小新 2021-01-31 19:25

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 p

6条回答
  •  花落未央
    2021-01-31 20:06

    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
    

提交回复
热议问题