Pippo's blog

it is all about software development

One more for heroku

Horn making noise! Something wrong happened in production! Taking a look in the logs it seems to be some bad data in the database making the app misbehave, just a internal API which wasn’t being carefully handling exceptions with unexpected data.

First thought was: probably the last code we deployed to STAGING is good enough and can better handle the exception.

How to make sure of that? It is a rails app so I want to deploy new code to the server and migrate the DB schema to the last one. How can I be sure I wont waste my time deploying the new version and it gonna work with the DB with the current state?

Heroku helped me a lot on that, these are the steps I took:

  • Backing up the PROD DB
1
2
3
heroku addons:add pgbackups --app PROD
heroku pgbackups:capture --app PROD
heroku pgbackups:url --app PROD # => DUMP_FILE_URL
  • Instantiate a new postgresql instance for your staging app
1
2
3
4
heroku addons:add heroku-postgresql:dev # => NEW_DB_URL
heroku addons:add pgbackups --app STAGING
heroku pgbackups:restore NEW_DB_URL <DUMP_FILE_URL>
heroku config:set DATABASE_URL=<NEW_DB_URL> # next time your app deploy it will point to the new DB
  • New deploy and migration We do have in this project a rake task that handles deployment steps for me including db migration so I run:
1
rake deploy:staging

or if you don’t have similar thing:

1
2
3
4
heroku maintenance:on --app STAGING
git push staging master
heroku run db:migrate --app STAGING
heroku maintenance:off --app STAGING
  • Smoke tests Check if everything looks good

  • Production deploy Now you know if a new deploy will make any damage to your production server, in case we are good:

1
rake deploy:production

… which does similar steps as above, but for production app instead!

Comments