Imagine you have some type of demo that needs to reset itself once a day.
-
Install the Database Cleaner Gem and make sure you make it available for the production environment.
-
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
-
Commit and do
git push heroku master
-
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
-
Test it by running
heroku run rake reset_demo
-
Use the Heroku Scheduler Add-on to program how often should the rake task be executed.