Tags:
Programming
Ruby on Rails
I'm posting this so that it can be Googled by others having the same/similar problem.
In both Rails 1.2 and Rails 2.1 on Mac and Windows, I hit a problem in the ActiveRecord migration code which causes a database migration to abort. Here is the rake db:migrate --trace output:
Macintosh:geekblog me$ rake db:migrate --trace
(in /Users/me/rails/geekblog)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** has_many_polymorphs: rails environment detected
** has_many_polymorphs: preloading parent model Tag
** has_many_polymorphs: preloading parent model Tag
** Execute db:migrate
rake aborted!
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.info
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/migration.rb:421:in `migrate'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/migration.rb:420:in `each'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/migration.rb:420:in `migrate'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/migration.rb:357:in `up'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/migration.rb:340:in `migrate'
/Library/Ruby/Gems/1.8/gems/rails-2.1.0/lib/tasks/databases.rake:99
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:546:in `call'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:546:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:541:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:541:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:508:in `invoke_with_call_chain'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:501:in `synchronize'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:501:in `invoke_with_call_chain'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:494:in `invoke'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:1931:in `invoke_task'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:1909:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:1909:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:1909:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:1948:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:1903:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:1881:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:1948:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake.rb:1878:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.8.1/bin/rake:31
/usr/bin/rake:19:in `load'
/usr/bin/rake:19
On line 421 of /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/migration.rb, we find this:
Base.logger.info "Migrating to #{migration} (#{migration.version})"
At this point, Base.logger is always null (nil) in all my Rails projects, at versions 1.2 and 2.1, on Mac and Windows, in development and production environments. I don't know why... But the fix is simple. Change the above line so that it checks to see if the logger is null (which it should probably be doing):
Base.logger.info "Migrating to #{migration} (#{migration.version})" unless Base.logger.nil?
Since this "bug" has persisted for years, I assume this is a problem that others are not hitting. Logging works in my development and production environments, so logger should not be null in this case.
*shrug*
Life goes on.
Anyone else hitting this problem with rake db:migrate? Let me know so we can bond and so on.
rake rails:freeze:gems
so that I was running rails out of vendor/plugins instead of the system-wide installation. Then I edited the line as described above, which makes my set-up now work. UUUUUUGLY, but I hope that (if more people find this post via Google) they may find this comment useful.
Thanks for the work-around, Leland! I didn't know you could run rails out of your own plugins directory. Great tip!
Post a comment: