Class: AddCredsCounterCache

Inherits:
ActiveRecord::Migration
  • Object
show all
Defined in:
db/migrate/20130423211152_add_creds_counter_cache.rb

Instance Method Summary collapse

Instance Method Details

#downObject



21
22
23
# File 'db/migrate/20130423211152_add_creds_counter_cache.rb', line 21

def down
  remove_column :hosts, :cred_count
end

#upObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'db/migrate/20130423211152_add_creds_counter_cache.rb', line 2

def up
  add_column :hosts, :cred_count, :integer, :default => 0
  Mdm::Host.reset_column_information
  # Set initial counts
  cred_service_ids = Set.new
  Mdm::Cred.all.each {|c| cred_service_ids << c.service_id}
  cred_service_ids.each do |service_id|
    #Mdm::Host.reset_counters(Mdm::Service.find(service_id).host.id, :creds)
    begin
      host = Mdm::Service.find(service_id).host
    rescue
      next
    end
    next if host.nil? # This can happen with orphan creds/services
    host.cred_count = host.creds.count
    host.save
  end
end