Search code examples
rubyrakervmdatabase-migrationgithooks

Using rake db:migrate in git hook - undefined class/module Encoding


I'm using https://github.com/thuss/standalone-migrations to perform db migrations in a cakephp environment. I am ultimately trying to perform db migrations automatically after checking out different versions of the code by using the post-checkout git hook.

Running normally on the cli bundle exec rake db:migrate works fine, as does rake db:migrate or any of the other commands. If I put the command in .git/hooks/post-checkout it spews an error complaining of undefined class/module Encoding

The git hook command is bundle exec rake db:migrate --trace.

Platform: Mac OS X Lion 10.7.5

This is the entire error:

rake aborted!
undefined class/module Encoding
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/json-1.7.5/lib/json/ext/parser.bundle
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/json-1.7.5/lib/json/ext.rb:13
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/json-1.7.5/lib/json.rb:58:in `require'
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/json-1.7.5/lib/json.rb:58
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/core_ext/object/to_json.rb:3:in `require'
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/core_ext/object/to_json.rb:3
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/core_ext/object.rb:10:in `require'
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/core_ext/object.rb:10
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/core_ext.rb:2:in `require'
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/core_ext.rb:2
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/core_ext.rb:1:in `each'
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/core_ext.rb:1
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/all.rb:3:in `require'
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/all.rb:3
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/standalone_migrations-2.0.1/lib/standalone_migrations/configurator.rb:1:in `require'
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/standalone_migrations-2.0.1/lib/standalone_migrations/configurator.rb:1
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/standalone_migrations-2.0.1/lib/standalone_migrations.rb:8:in `require'
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/standalone_migrations-2.0.1/lib/standalone_migrations.rb:8
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/standalone_migrations-2.0.1/lib/tasks/standalone_migrations.rb:9:in `require'
/Users/andy/.rvm/gems/ruby-1.9.3-p194/gems/standalone_migrations-2.0.1/lib/tasks/standalone_migrations.rb:9
/Users/andy/source/idio/cake/Rakefile:6:in `require'
/Users/andy/source/idio/cake/Rakefile:6
/Users/andy/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load'
/Users/andy/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load_rakefile'
/Users/andy/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:501:in `raw_load_rakefile'
/Users/andy/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:82:in `load_rakefile'
/Users/andy/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/andy/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:81:in `load_rakefile'
/Users/andy/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:65:in `run'
/Users/andy/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/andy/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/usr/bin/rake:31

I'm not too hot with ruby. I've tried all sorts of tricks, deleted all gems, reinstalled etc. Always the same.

Thanks.


Solution

  • Turns out this is an issue with git re-appending /usr/bin to $PATH within the hook. This causes the OS version of ruby (located in /usr/bin) to be called instead of my rvm version. OS version is 1.8.7, doesn't have the right gems etc. I'm using 1.9.3.

    To fix you need to source the rvm setup script within the hook. Something like:

    #!/usr/bin/env bash
    [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
    
    # ...rest of the post commit commands