Reset database with rake task

江枫思渺然 提交于 2019-12-13 16:51:29

问题


I want to use Heroku's scheduler to reset my database once every day.

It's recommended to use rake tasks for the scheduler. This is what I've tried:

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

But how would I do this correctly, because putting the heroku commands within backticks, which with bash normally works, doesn't work here:

No such file or directory - heroku


回答1:


Try this rake task:

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


来源:https://stackoverflow.com/questions/28515857/reset-database-with-rake-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!