Most of my projects lately have been deployed on Heroku. They’ve developed a really nice set of tools to get your Rails (and other) apps from a git repo out into the world. They do really smart things regarding database connections to make things easy to push live. If you follow the standard setup, you’ll be running on Postgres hosted at Heroku.
Often, we want to take data that may be out on a live app (staging or production level) and setup a development machine to have that data. For complex data models and complex data setups, this can be the only way to debug issues that may not have been covered by standard unit/integration tests. With PGBackups, a heroku add-on, and a couple small Rake tasks, this is a snap.
First, make sure you have pgbackups setup for your app:
% heroku addons:add pgbackups
With this enabled, you can immediately grab a snapshot of the database
You can also easily setup auto-weekly or auto-monthly snapshots
heroku addons:add pgbackups:auto-week heroku addons:add pgbackups:auto-month
To grab the database, you ask for the url
which you can then pull down with curl or by some other means. Then use
pg_restore to jam it into your local database (if you want).
To make this even easier, I wrote a couple rake tasks which i bring along from project to project. To make it even easier.
Drop this file in your lib/tasks/ directory, update the namespace and development database name and you’re off to the races:
With this in place, you should be able to do:
rake my_app_namespace:db:fetch app=my_heroku_app_name rake my_app_namespace:restore dbfile=the_dump_file_from_the_previous_command
The other task in there may not suit everyone’s purposes, but if you have sensitive User data, you can use that task to clean it up. Currently it just changes passwords to ‘monkey’. But you could do other things, like set all emails to or remove phone numbers or any other bits that you might not want to be real on your development system.
Now that I’ve done this a couple times, the next step to improve things is to get the script to read the local database.yml to grab the development database name automagically. An easy addition if you want to streamline things even more.