2

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

Community
  • 1
  • 1
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232

1 Answers1

2

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
wintermeyer
  • 8,178
  • 8
  • 39
  • 85
  • Unfortunately the `load_seed` method loops my seed files infinitely. (I use this code for my seed files: http://stackoverflow.com/a/19872375/3205492) – Fellow Stranger Feb 14 '15 at 14:25
  • 1
    I changed the answer to include the possibility to start an other rake task. In case you have a more complicated problem you need to create a new question with all the code involved. – wintermeyer Feb 14 '15 at 18:25
  • Genius! Thanks a lot! I only had to put the other rake tasks outside the each block. – Fellow Stranger Feb 14 '15 at 19:21