7
7

More than 5 years have passed since last update.

heroku run rails db:migrateの時にpg is not part of the bundle

Posted at

はじめに

Cloud9でRuby on Railsのアプリを作っています。
rails tutorialを参考に開発しており、herokuで動作確認をしながら開発を進めていたものの、
突然 heroku run rails db:migrate が出来なくなってしまいだいぶはまってしまいました。。。

環境

IDE:cloud9
リポジトリ管理:bitbucket
本番環境:heroku
Ruby: 2.4.1
Rails: 5.2.1

状況

heroku run rails db:migrateをすると

rails aborted!
Gem::LoadError: Error loading the 'postgresql' Active Record adapter. Missing a gem it depends on? pg is not part of the bundle. Add it to your Gemfile.

と言うエラーメッセージが出ました。
pgをGemfileに追加しろ?追加しとるわ!

pgが本当にheroku上のGemfile.lockに入っているのか、
pgのバージョンがおかしくないか、database.ymlはおかしくないかと色々見直しました。
heroku run rails db:migrateを何十回打ったことか。。。

解決策

.bundle/configをherokuにアップロードしてしまったために、(もちろん.gitignoreに書いてますよ。。)

BUNDLE_WITHOUT: "production"

が本番環境(heroku)上でも適用され、production用のpgがherokuにダウンロードされなかったことのようです。

原因

あるタイミングでgit管理下に.bundle/configを一度置いてしまったみたいで、
gitのキャッシュが残ったままになっていました。
そのため、.gitignoreに記載したのに反映されない件
を参考にキャッシュを削除しました。

するとpgがheroku上でダウンロードされ、db:migrateまで出来ました。

背景

heroku run rails db:migrate前のgit push heroku実行時のログをもう一度じっくり
見てみると。。。

You have the `.bundle/config` file checked into your repository
It contains local state like the location of the installed bundle
as well as configured git local gems, and other settings that should
not be shared between multiple checkouts of a single repo. Please
remove the `.bundle/` folder from your repo and add it to your `.gitignore` file.
https://devcenter.heroku.com/articles/bundler-configuration

というwarningが。。。
これよく見たら問題ありそう。。
rails tutorialではwaringいくつか出ますが無視してOK!みたいなこと書いてたので鵜呑みにして
warningスルーしてたらいつもと違うの出てました。
ちゃんとメッセージ見ないとダメですね〜〜。。

.bundle/configは.gitignoreに書いてましたが、それがherokuにアップロードされているということは
.gitignoreが反映されてない???
そのまま感じたことをググったところ上記の記事にたどり着きました。
gitのキャッシュを削除し、解決に至りました。

まとめ

warningメッセージちゃんと見ないとダメですね。。。
あと、git にキャッシュという概念あるんですね。。。

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7