Herokuのasset_precompileとdb:pushに詰まる
久しぶりにHeroku使ったらいくつかこけたのでメモ。
- ・sqlite3で budle install 失敗
- ・rake asset:precompile 失敗
- ・tapsで rake db:push 失敗
sqlite3で $ budle install 失敗
appをherokuにpushしたところ…
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/usr/local/bin/ruby extconf.rb
checking for sqlite3.h... no
/usr/local/bin/ruby extconf.rb
checking for sqlite3.h... no
原因
gem 'sqlite3'を本番にも入れてた。
rake asset:precompileに失敗
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Tasks: TOP => environment
Running: rake assets:precompile
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Tasks: TOP => environment
対処方法
config/application.rbに下記を追記。
config.assets.initialize_on_precompile = false
ローカルでprecompile$ RAILS_ENV=production bundle exec rake assets:precompile
エラーメッセージに解説のURLが含まれています。asset precompileの際にDBを見に行くのですが、この時使用する環境変数がslug compile(Herokuへのpushをhookにした処理の呼び名)時には存在しないため、仮で用意した変数を見てlolhostのDBを見に行きエラーが起こるそうです。
tapsで rake db:push 失敗
dev環境でmysql→herokuでpostgresへの移行です。
原因
見落としてたポイントは3点
・rbenv+Heroku Toolbeltの組み合わせで発生するエラーがある
・localのherokuコマンドはlocalのgemのみを参照する
・使用するrdbmsのgemに加え、sqlite3のgemが必須
解決方法
bundleではなくlocalにgem"mysql2","taps(0.3.24)","sqlite3"をinstall
Heroku Toolbeltを削除しlocalにgem"heroku(2.33.0)"をinstall
いくつかミスが重なったので順に見ていきます。
rbenv+Heroku Toolbeltで起こるらしいエラー
heroku db:push mysql2://USER@localhost/APPをすると…
! Taps Load Error: cannot load such file -- taps/operation
! You may need to install or update the taps gem to use db commands.
! On most systems this will be:
!
! sudo gem install taps
tapsは当然インストール済みだし最新版。調べてみるとこんな記事が。! You may need to install or update the taps gem to use db commands.
! On most systems this will be:
!
! sudo gem install taps
下の方に何件かrbenv+Heroku Toolbeltでこのエラーの報告が。
Heroku Toolbeltを削除し、gem版herokuをinstall。deprecatedされてるものにあえて戻す
sqlite3は必須
再トライ
! Taps Load Error: cannot load such file -- sqlite3
! You may need to install or update the taps gem to use db commands.
! On most systems this will be:
sqlite3?今関係ないし、そもそも入ってるんだけど…! You may need to install or update the taps gem to use db commands.
! On most systems this will be:
と思ったら必須らしい+bundleで入れてlocalには入れてませんでした。
$ gem i sqlite3
やっと成功です。> app
Sending schema
Schema: 100% |==========================================| Time: 00:00:14
Sending indexes
Sending schema
Schema: 100% |==========================================| Time: 00:00:14
Sending indexes
おまけ:昔入れたHeroku Toolbelt使ってた
実は最初こんなミスをしていました。エラー発生
Failed to connect to database:
Sequel::AdapterNotFound -> LoadError: no such file to load -- mysql2
mysql2入ってるけど…と思って良くみるとSequel::AdapterNotFound -> LoadError: no such file to load -- mysql2
Loaded Taps v0.3.23
Tapsのバージョンが違う。$ heroku version
heroku-gem/2.33.0 (universal-darwin10.0) ruby/1.8.7
今はruby1.9.3系を使っています。以前入れたherokuを使っていて、その際のrubyのgemを参照していました。heroku-gem/2.33.0 (universal-darwin10.0) ruby/1.8.7
herokuをuninstallして、Heroku Toolbeltをinstall