So… I’ve got a migration (post.rb uses the state_machine gem)
class AddVerifiedAtToPost < ActiveRecord::Migration
def self.up
add_column :posts, :verified_at, :datetime
Post.find_each do |post|
post.update_attribute(:verified_at, post.created_at) unless post.awaiting_verification?
end
end
def self.down
remove_column :posts, :verified_at
end
end
… This gives me an undefined method “relation” for nil class…
So I thought– hmm, maybe for some weird reason the state_machine predicate methods are not working in the context of a migration, so I modified the code to do:
Post.find_each do |post|
post.update_attribute(:verified_at, post.created_at) unless post.status == "awaiting_verification"
end
But then I get the error of duplicate column “verified_at” already exists……. Ok— so then I wrap my add_column in a column_exists?, giving me a migration that now looks like:
class AddVerifiedAtToPost < ActiveRecord::Migration
def self.up
unless column_exists?(:posts, :verified_at)
add_column :posts, :verified_at, :datetime
end
Post.find_each do |post|
post.update_attribute(:verified_at, post.created_at) unless post.status == "awaiting_verification"
end
end
def self.down
remove_column :posts, :verified_at
end
end
… Run rake db:migrate… it succeeds.. hooray… But I am still skeptical, so I run rake db:migrate:down VERSION=
and then run rake db:migrate again, guess what?
== AddVerifiedAtToPost: migrating ============================================
-- column_exists?(:posts, :verified_at)
-> 0.0048s
-- add_column(:posts, :verified_at, :datetime)
-> 0.0644s
rake aborted!
An error has occurred, all later migrations canceled:
undefined method `column' for nil:NilClass
(See full trace by running task with --trace)
… I run rake db:migrate again, it works… I do rake db:migrate:down again, and then rake db:migrate– get the same error……..
So I tried an experiment.. I ran rake db:migrate:down, removed the column.. Hopped into the console and did ActiveRecord::Migration.add_column(:posts, :verified_at, :datetime)
Ran the migration– no errors.. It seems that for some reason add_column in the context of this migration file is being all weirdo on me.
this is the stack trace:
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
== AddVerifiedAtToPost: migrating ============================================
-- column_exists?(:posts, :verified_at)
-> 0.0052s
-- add_column(:posts, :verified_at, :datetime)
-> 0.0805s
rake aborted!
An error has occurred, all later migrations canceled:
undefined method `column' for nil:NilClass
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/whiny_nil.rb:48:in `method_missing'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/nodes/unqualified_column.rb:8:in `column'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:249:in `visit_Arel_Nodes_Assignment'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in `send'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in `visit'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/mysql.rb:25:in `visit_Arel_Nodes_UpdateStatement'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/mysql.rb:25:in `map'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/mysql.rb:25:in `visit_Arel_Nodes_UpdateStatement'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in `send'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in `visit'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:5:in `accept'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:18:in `accept'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:111:in `with_connection'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:16:in `accept'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/tree_manager.rb:20:in `to_sql'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/arel-2.0.10/lib/arel/crud.rb:20:in `update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/persistence.rb:255:in `update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/locking/optimistic.rb:77:in `update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/attribute_methods/dirty.rb:68:in `update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/timestamp.rb:60:in `update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/callbacks.rb:281:in `update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:419:in `_run_update_callbacks'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/callbacks.rb:281:in `update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/persistence.rb:246:in `create_or_update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/callbacks.rb:273:in `create_or_update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:456:in `_run_save_callbacks'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:221:in `_conditional_callback_around_270'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/integrations/active_record.rb:383:in `around_save'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:149:in `run_actions'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:169:in `catch_exceptions'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:147:in `run_actions'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:132:in `run_callbacks'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:211:in `run_callbacks'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:63:in `perform'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:63:in `catch'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:63:in `perform'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:185:in `within_transaction'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/transition_collection.rb:62:in `perform'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/state_machine-1.0.0/lib/state_machine/integrations/active_record.rb:383:in `around_save'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:315:in `send'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:315:in `_callback_around_268'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:220:in `_conditional_callback_around_270'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:414:in `_run_save_callbacks'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/callbacks.rb:273:in `create_or_update'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/persistence.rb:39:in `save'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/validations.rb:43:in `save'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/attribute_methods/dirty.rb:21:in `save'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/transactions.rb:240:in `save_without_unsaved_flag'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/transactions.rb:292:in `with_transaction_returning_status'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/transactions.rb:207:in `transaction_without_trace_ActiveRecord_self_name_transaction'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/bundler/gems/rpm-f41cc20ce8b6/lib/new_relic/agent/method_tracer.rb:491:in `transaction'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/bundler/gems/rpm-f41cc20ce8b6/lib/new_relic/agent/method_tracer.rb:242:in `trace_execution_scoped'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/bundler/gems/rpm-f41cc20ce8b6/lib/new_relic/agent/method_tracer.rb:486:in `transaction'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/transactions.rb:240:in `save_without_unsaved_flag'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/transactions.rb:251:in `rollback_active_record_state!'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/transactions.rb:239:in `save_without_unsaved_flag'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/active_scaffold_vho-3.0.20/lib/active_scaffold/extensions/unsaved_record.rb:15:in `save'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/persistence.rb:117:in `update_attribute'
/Users/patrick/coding/rails/bounty_buy/myapp-v1/db/migrate/20120123171827_add_verified_at_to_post.rb:9:in `up_without_benchmarks'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/relation/batches.rb:21:in `find_each'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/relation/batches.rb:21:in `each'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/relation/batches.rb:21:in `find_each'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/relation/batches.rb:71:in `find_in_batches'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/relation/batches.rb:20:in `find_each'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/base.rb:441:in `__send__'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/base.rb:441:in `find_each'
/Users/patrick/coding/rails/bounty_buy/myapp-v1/db/migrate/20120123171827_add_verified_at_to_post.rb:8:in `up_without_benchmarks'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:312:in `send'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:312:in `migrate'
/Users/patrick/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/benchmark.rb:293:in `measure'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:312:in `migrate'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:395:in `__send__'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:395:in `migrate'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:537:in `migrate'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:613:in `call'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:613:in `ddl_transaction'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:536:in `migrate'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:523:in `each'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:523:in `migrate'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:433:in `up'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/migration.rb:415:in `migrate'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@myapp/gems/activerecord-3.0.7/lib/active_record/railties/databases.rake:142
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/Users/patrick/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/gems/rake-0.8.7/bin/rake:31
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/bin/rake:19:in `load'
/Users/patrick/.rvm/gems/ree-1.8.7-2011.03@global/bin/rake:19
note, I also commented out all the post model’s before/after save/create callbacks to see if that was some how triggering this, and it’s not…
Call “reset_column_information” before playing with changed columns