ローカルの開発環境
データベースのパスワードなどを平文でdatabase.ymlに保存したくないので、
credentials.yml.encにdatabaseのパスワードなどを保存している。
↓
【備忘録】credentials.yml.encにdatabase設定を保存する
heroku
この環境のままherokuにデプロイし、db:migrateするとherokuのdbに接続できず、エラーになる。
$ heroku rake db:migrate
....
rake aborted!
NoMethodError: Cannot load database configuration:
undefined method `[]' for nil:NilClass
このままheroku openすると以下のようなエラーになってApp Crashedになる。
heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=violao.herokuapp.com request_id=bf9aba32-8c1e-4b44-948b-3d302934f41f fwd="XXX.XXX.XXX.XXX" dyno= connect= service= status=503 bytes= protocol=https
解決方法
master.key をherokuの環境変数としてセットする。
$ heroku config:set RAILS_MASTER_KEY=`cat config/master.key`
Setting RAILS_MASTER_KEY and restarting ⬢ app_name... done, v13
RAILS_MASTER_KEY: xxxxxx601433892f64d2c33e69cxxxxxx
↓参考
secrets.ymlや環境変数をRails 5.2のEncrypted Credentialsに移行する
その後 heroku db:migrateでdbのマイグレーションを行うと、エラーが解消し、heroku openでアプリが起動する。
$ heroku rake db:migrate