ruby-on-railsherokudata-migrationheroku-pipelines

Migrating Data on Heroku Pipeline Promotion


I'm using a pipeline for my rails app on Heroku. I have a staging app, which is then promoted to a production app. I run any schema migrations before deploying to staging and the slug is then moved to production when I promote it.

But now need I to migrate some user data on my next deploy, and I'm looking for a way to do this as part of the promotion. I could always run a rake task when the promotion is complete, but that seems very risky. Ideally, the code should run before the app is released, and rollback if anything goes wrong.

I thought that Release Phases were the answer, but I don't think I can access user data there, given these considerations.


Solution

  • It seems that my reservations about Release Phases were unfounded. You can migrate data just fine as part of a release phase. For anyone else trying the same thing, I used the data_migrate ruby gem, which works very well. I then updated my Procfile to include the following:

    release: rails db:migrate:with_data
    

    This runs any data migrations that I've created with the gem, and if anything goes wrong there, the release is aborted.