在 git hook 中使用 rake db:migrate - 未定义的类/模块编码
Posted
技术标签:
【中文标题】在 git hook 中使用 rake db:migrate - 未定义的类/模块编码【英文标题】:Using rake db:migrate in git hook - undefined class/module Encoding 【发布时间】:2012-09-30 19:05:14 【问题描述】:我正在使用https://github.com/thuss/standalone-migrations 在 cakephp 环境中执行数据库迁移。我最终试图在使用 post-checkout git hook 签出不同版本的代码后自动执行数据库迁移。
在 cli bundle exec rake db:migrate
上正常运行,rake db:migrate
或任何其他命令都可以正常运行。如果我将命令放在.git/hooks/post-checkout
中,它会发出一个错误,抱怨undefined class/module Encoding
git hook 命令是bundle exec rake db:migrate --trace
。
平台:Mac OS X Lion 10.7.5
这是整个错误:
耙中止! 未定义的类/模块编码 /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我不太喜欢红宝石。我尝试了各种技巧,删除了所有的 gem,重新安装了等等。总是一样的。
谢谢。
【问题讨论】:
【参考方案1】:原来这是 git 在钩子中将 /usr/bin 重新附加到 $PATH 的问题。这会导致调用 ruby 的操作系统版本(位于 /usr/bin 中)而不是我的 rvm 版本。操作系统版本是 1.8.7,没有正确的 gem 等。我使用的是 1.9.3。
要修复,您需要在挂钩中获取 rvm 设置脚本。比如:
#!/usr/bin/env bash
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
# ...rest of the post commit commands
【讨论】:
以上是关于在 git hook 中使用 rake db:migrate - 未定义的类/模块编码的主要内容,如果未能解决你的问题,请参考以下文章