使用rake任务重置数据库

时间:2015-02-14 12:55:26

标签: ruby-on-rails heroku rake

我想每天使用Heroku的scheduler重置一次数据库。

建议对调度程序使用rake任务。这就是我尝试过的:

task :reset_database => :environment do
  `heroku pg:reset MY_DB:URL`
  `heroku run rake db:migrate db:seed`
  # some other ruby commands
end

但是我怎么能正确地做到这一点,因为将heroku命令放在反引号中,而bash normally works在这里不起作用:

No such file or directory - heroku

1 个答案:

答案 0 :(得分:2)

尝试此rake任务:

namespace :reset_database do
  desc "Destroy all table entries."
  task :all => :environment do
    ActiveRecord::Base.connection.tables.each do |table|
      if table != 'schema_migrations'
        table.singularize.camelize.constantize.destroy_all
      end
      # Use this if you want to use the normal seeds:
      # Rails.application.load_seed

      # Use this if you want to run another rake task:
      Rake::Task["foo:bar"].invoke
    end
  end
end