parent
a958f2fa3f
commit
f19cbcb1c6
@ -0,0 +1,24 @@
|
||||
class AddEvaluateCountToShixuns < ActiveRecord::Migration
|
||||
def up
|
||||
add_column :shixuns, :evaluate_count, :integer, default: 0
|
||||
|
||||
limit = 100
|
||||
total = Shixun.count
|
||||
|
||||
((total / 100) + 1).times do |index|
|
||||
shixun_ids = Shixun.order(:id).limit(limit).offset(index * limit).pluck(:id)
|
||||
return if shixun_ids.blank?
|
||||
|
||||
count_map = Game.joins(:challenge).where(challenges: { shixun_id: shixun_ids }).group('shixun_id').sum(:evaluate_count)
|
||||
|
||||
id_str = count_map.keys.join(',')
|
||||
str = count_map.map { |shixun_id, count| "WHEN #{shixun_id} THEN #{count}" }.join(' ')
|
||||
|
||||
execute "UPDATE shixuns SET evaluate_count = CASE id #{str} END WHERE id IN (#{id_str})"
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
remove_column :shixuns, :evaluate_count
|
||||
end
|
||||
end
|
Loading…
Reference in new issue