How to reset Heroku databases with the Scheduler

Posted by Max Kuipers on Nov 17, 2015 10:40:07 AM

If you're using a standard Heroku setup with a postgres database, resetting the database is usually as easy as issuing the command heroku pg:reset DATABASE. However, if you want to schedule a daily reset with the Heroku Scheduler, you'll have to get a bit more creative since you are unable to call heroku pg:reset DATABASE from a Heroku instance.

Wait, why not just use rake db:reset?

Most people have tried to use a rake db:reset on a Heroku and seen an error that looks something like:

I don't want to get into the details of how postgres permissions on Heroku work, but the short of it is that this error is raised because db:reset is actually just a combination of db:drop, db:create, db:schema:load, and db:seed in that order. The error is raised when the Heroku instance tries to run db:drop or db:create. Unfortunately, these are the parts that actually wipe the database.

So we can't schedule pg:reset nor db:reset, whatever will we do?

Fear not! The indomitable will of the UrbanBound hackers is not to be underestimated! We came up with this janky rake task to accomplish an approximation of the same database wipe effect that we would normally get from db:drop and db:create.

This rake task drops all the tables. When used in combination with db:schema:load and db:seed you can get a reasonable approximation of db:reset for use in a scheduled reset.

Topics: rails, rake, postgres, heroku

Subscribe to the UrbanBound MAKE blog

Recent Posts

Human Resources Today