Skip to content

Instantly share code, notes, and snippets.

@serodriguez68
Last active January 18, 2019 07:07
Show Gist options
  • Save serodriguez68/8b9c9597a0071437a691fbf4dd502373 to your computer and use it in GitHub Desktop.
Save serodriguez68/8b9c9597a0071437a691fbf4dd502373 to your computer and use it in GitHub Desktop.
How to make a Rails application rails db:reset itself on Heroku

How to make a Rails application rails db:reset itself on Heroku

Imagine you have some type of demo that needs to reset itself once a day.

  1. Install the Database Cleaner Gem and make sure you make it available for the production environment.

  2. Create a lib/tasks/scheduler.rake file that looks similar to this

desc "This task is called by the Heroku scheduler add-on to reset the demo"
task :reset_demo => :environment do
  puts "Cleaning Up The DB..."
  DatabaseCleaner.strategy = :truncation
  DatabaseCleaner.clean
  puts "Seeding the DB again..."
  Rake::Task["db:seed"].invoke
  puts "done!"
end
  1. Commit and do git push heroku master

  2. Add the following ENV variables to your Heroku app to disable the Database Cleaner Safeguards. You can do this by logging into your Heroku account.

DATABASE_CLEANER_ALLOW_PRODUCTION=true
DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL=true
  1. Test it by running heroku run rake reset_demo

  2. Use the Heroku Scheduler Add-on to program how often should the rake task be executed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment