Project

General

Profile

Actions

Support #17105

closed

undefined method `counter_cache_column' for nil:NilClass after upgrade

Added by Va rac over 7 years ago. Updated over 7 years ago.

Status:
Resolved
Priority:
Normal
Assignee:
Category:
DB migrations
Target version:
-
Triaged:
Fixed in Releases:
Found in Releases:

Description

I am using foreman on debian jessie, using the deb package repository.
We didn't upgrade the foreman packages for a while (yeah, i know we should probably
upgrade more frequently).
This resulted in an upgrade from foreman version 1.8.3-1 to 1.13.0-1.
I followed https://theforeman.org/manuals/1.13/index.html#3.6Upgrade but "Step 3 (A) - Database migration and cleanup"
failes for us with this error:

root@foreman:~# foreman-rake db:migrate
== 20151104100257 AddHostsCountToHostgroup: migrating =========================
-- add_column(:hostgroups, :hosts_count, :integer, {:default=>0})
   -> 0.2647s
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

undefined method `counter_cache_column' for nil:NilClass
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/counter_cache.rb:38:in `block in reset_counters'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/counter_cache.rb:22:in `each'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/counter_cache.rb:22:in `reset_counters'
/usr/share/foreman/db/migrate/20151104100257_add_hosts_count_to_hostgroup.rb:5:in `block in up'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/relation/delegation.rb:46:in `each'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/relation/delegation.rb:46:in `each'
/usr/share/foreman/db/migrate/20151104100257_add_hosts_count_to_hostgroup.rb:4:in `up'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:611:in `exec_migration'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:591:in `block in migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:590:in `migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:768:in `migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:1044:in `block in ddl_transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/transactions.rb:220:in `transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:1044:in `ddl_transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:959:in `block in migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:955:in `each'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:955:in `migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:823:in `up'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:801:in `migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
NoMethodError: undefined method `counter_cache_column' for nil:NilClass
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/counter_cache.rb:38:in `block in reset_counters'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/counter_cache.rb:22:in `each'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/counter_cache.rb:22:in `reset_counters'
/usr/share/foreman/db/migrate/20151104100257_add_hosts_count_to_hostgroup.rb:5:in `block in up'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/relation/delegation.rb:46:in `each'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/relation/delegation.rb:46:in `each'
/usr/share/foreman/db/migrate/20151104100257_add_hosts_count_to_hostgroup.rb:4:in `up'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:611:in `exec_migration'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:591:in `block in migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:590:in `migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:768:in `migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:1044:in `block in ddl_transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/transactions.rb:220:in `transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:1044:in `ddl_transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:959:in `block in migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:955:in `each'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:955:in `migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:823:in `up'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:801:in `migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/usr/share/foreman/vendor/ruby/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

I found #7732 and #11044 which reported similar problems but none of them show me how I can fix this problem.

Actions #1

Updated by Ohad Levy over 7 years ago

migration from 1.8 directly to 1.13 is not supported.

however, as a workaround, you may try to apply the following patch:

diff --git a/db/migrate/20151104100257_add_hosts_count_to_hostgroup.rb b/db/migrate/20151104100257_add_hosts_count_to_hostgroup.rb
index 04f52e3..48df7c9 100644
--- a/db/migrate/20151104100257_add_hosts_count_to_hostgroup.rb
+++ b/db/migrate/20151104100257_add_hosts_count_to_hostgroup.rb
@@ -2,7 +2,7 @@ class AddHostsCountToHostgroup &lt; ActiveRecord::Migration
   def up
     add_column :hostgroups, :hosts_count, :integer, :default =&gt; 0
     Hostgroup.all.each do |hg|
-      Hostgroup.reset_counters(hg.id, :hosts)
+      Hostgroup.reset_counters(hg.id, :hosts) rescue nil
     end
   end

Actions #2

Updated by Va rac over 7 years ago

Thx @Ohad, this fixed my broken upgrade. Awesome!

Actions #3

Updated by Anonymous over 7 years ago

  • Tracker changed from Bug to Support
  • Status changed from New to Resolved
  • Assignee set to Ohad Levy
Actions

Also available in: Atom PDF