2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
class DenormalizeExpressions < ActiveRecord::Migration[4.2]
|
2013-02-05 14:16:51 -05:00
|
|
|
def change
|
|
|
|
# Denormalizing this makes our queries so, so, so much nicer
|
|
|
|
|
|
|
|
add_column :posts, :expression1_count, :integer, null: false, default: 0
|
|
|
|
add_column :posts, :expression2_count, :integer, null: false, default: 0
|
|
|
|
add_column :posts, :expression3_count, :integer, null: false, default: 0
|
|
|
|
add_column :posts, :expression4_count, :integer, null: false, default: 0
|
|
|
|
add_column :posts, :expression5_count, :integer, null: false, default: 0
|
|
|
|
|
|
|
|
add_column :forum_threads, :expression1_count, :integer, null: false, default: 0
|
|
|
|
add_column :forum_threads, :expression2_count, :integer, null: false, default: 0
|
|
|
|
add_column :forum_threads, :expression3_count, :integer, null: false, default: 0
|
|
|
|
add_column :forum_threads, :expression4_count, :integer, null: false, default: 0
|
|
|
|
add_column :forum_threads, :expression5_count, :integer, null: false, default: 0
|
|
|
|
|
|
|
|
(1..5).each do |i|
|
|
|
|
execute "update posts set expression#{i}_count = (select count(*) from expressions where parent_id = posts.id and expression_type_id = #{i})"
|
|
|
|
execute "update forum_threads set expression#{i}_count = (select sum(expression#{i}_count) from posts where forum_thread_id = forum_threads.id)"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|