Added MySQL-friendly version of SQL code for migration.

This commit is contained in:
Pistos 2011-12-15 15:59:42 -05:00
parent f83e56e5f3
commit 5110b4c086

View file

@ -4,6 +4,7 @@ class CounterCacheOnPostReshares < ActiveRecord::Migration
def self.up def self.up
add_column :posts, :reshares_count, :integer, :default => 0 add_column :posts, :reshares_count, :integer, :default => 0
if postgres?
execute %{ execute %{
UPDATE posts UPDATE posts
SET reshares_count = ( SET reshares_count = (
@ -14,6 +15,18 @@ class CounterCacheOnPostReshares < ActiveRecord::Migration
AND p2.root_guid = posts.guid AND p2.root_guid = posts.guid
) )
} }
else # mysql
execute "CREATE TEMPORARY TABLE posts_reshared SELECT * FROM posts WHERE type = 'Reshare'"
execute %{
UPDATE posts p1
SET reshares_count = (
SELECT COUNT(*)
FROM posts_reshared p2
WHERE p2.root_guid = p1.guid
)
}
end
end end
def self.down def self.down