20131227000021_add_cached_dates_to_agent.rb 600 B

1234567891011121314
  1. class AddCachedDatesToAgent < ActiveRecord::Migration
  2. def up
  3. add_column :agents, :last_event_at, :datetime
  4. execute "UPDATE agents SET last_event_at = (SELECT created_at FROM events WHERE events.agent_id = agents.id ORDER BY id DESC LIMIT 1)"
  5. add_column :agents, :last_error_log_at, :datetime
  6. execute "UPDATE agents SET last_error_log_at = (SELECT created_at FROM agent_logs WHERE agent_logs.agent_id = agents.id AND agent_logs.level >= 4 ORDER BY id DESC LIMIT 1)"
  7. end
  8. def down
  9. remove_column :agents, :last_event_at
  10. remove_column :agents, :last_error_log_at
  11. end
  12. end