1

I'm running a small demo site. I'd like to reset the database once an hour or so (automatically, not manually). I could write a rake task to destroy records for each of the models. Is there a simpler approach?

How best to accomplish this?

klenwell
  • 6,978
  • 4
  • 45
  • 84

5 Answers5

2

Please refer this link to how to automate rake task on heroku http://kakimotonline.com/2014/04/27/using-rake-to-automate-heroku-tasks/

Your rake file will look something like this:

namespace :heroku do


  def run_command(cmd, app_name)
    Bundler.with_clean_env do
      sh build_command(cmd, app_name)
    end
  end

  def build_command(cmd, app_name)
    "heroku #{cmd} --app #{app_name}"
  end

end
klenwell
  • 6,978
  • 4
  • 45
  • 84
Akshay Borade
  • 2,442
  • 12
  • 26
  • Your answer looks closest to answering my question. The [link](http://kakimotonline.com/2014/04/27/using-rake-to-automate-heroku-tasks/) you cite at the end of your answer, with the `build command` task, looks like it could work for me. The `migrate` and `restart` commands in the code you copy-pasted are irrelevant. As is the scheduler gem (Heroku provides a scheduler). If you want to clean this up, and I can confirm it works, I'll accept this. – klenwell Jun 02 '15 at 15:17
  • In the end, your answer didn't fully answer my question. But I gave you an upvote for helping me toward a solution. – klenwell Jun 04 '15 at 01:16
2

You can use Heroku Scheduler to schedule a rake task. If you rake task runs with heroku run then it works with scheduler too.

Florent Ferry
  • 1,397
  • 1
  • 10
  • 21
1

Following the example linked by Akshay, I was able to get this working, although not exactly as I planned.

First, a clarification. To add the scheduler add-on on Heroku, you have to provide your credit card number. I'm still getting my bearings with the Heroku platform and am not ready to do that quite yet. So my solution works without the scheduler add-on. Instead it runs as a rake task from your local dev environment and uses the heroku toolbelt to run the reset command on your remote app.

If you are looking to run everything on Heroku, I don't think this will work as you can't run the heroku pg:reset command on Heroku itself. However, it looks like this solution here would work.

Now to my solution.

First, create the following rake file:

# lib/tasks/heroku.rake
namespace :heroku do

  # bundle exec rake heroku:reset_db['my-app-name']
  # Note: run locally with Heroku toolbelt to reset DB on app
  desc 'Reset database with seed data'
  task :reset_db, [:app_name] do |t, args|
    run_command("pg:reset DATABASE_URL --confirm #{args.app_name}", args.app_name)
    run_command("run rake db:migrate", args.app_name)
    run_command("run rake db:seed", args.app_name)
  end

  # Helper Functions
  # Source: http://kakimotonline.com/2014/04/27/using-rake-to-automate-heroku-tasks/
  def run_command(cmd, app_name)
    Bundler.with_clean_env do
      sh build_command(cmd, app_name)
    end
  end

  def run_command_with_output(cmd, app_name)
    Bundler.with_clean_env do
      `#{build_command(cmd, app_name)}`
    end.gsub("\n", "")
  end

  def build_command(cmd, app_name)
    "heroku #{cmd} --app #{app_name}"
  end
end

Now you can reset your database by running the following rake command from your local environment with your app name (run heroku apps for your app name):

bundle exec rake heroku:reset_db['my-app-name']

And if you want to schedule it to run at regular intervals as I did, add a job to your local crontab (which is a little more complicated than just copying the line above):

# Reset Heroku database hourly
RBENV_SHIM=/home/klenwell/.rbenv/shims/
RBENV_BIN=/home/klenwell/.rbenv/bin
RAILS_APP=/home/klenwell/projects/my-app-name
HEROKU_LOG=/tmp/heroku-cron.log
0 * * * *      export PATH=$RBENV_SHIM:$RBENV_BIN:$PATH; eval "$(rbenv init -)"; cd $RAILS_APP && bundle exec rake heroku:reset_db['my-app-name'] >> $HEROKU_LOG 2>&1

For more info on setting up a cron job to run a rake task with rbenv, see this article.

Community
  • 1
  • 1
klenwell
  • 6,978
  • 4
  • 45
  • 84
0

rake db:schema:load accomplishes what you want, though I've never tried running it on Heroku.

You could also try the database_cleaner gem, which is typically used in test environments.

Kristján
  • 18,165
  • 5
  • 50
  • 62
0

Try heroku run rake db:setup. But you will have to run it manually anytime you need to.

Mike McCallen
  • 307
  • 1
  • 14