Added MySQL-friendly version of SQL code for migration.
This commit is contained in:
parent
f83e56e5f3
commit
5110b4c086
1 changed files with 23 additions and 10 deletions
|
|
@ -4,16 +4,29 @@ class CounterCacheOnPostReshares < ActiveRecord::Migration
|
|||
def self.up
|
||||
add_column :posts, :reshares_count, :integer, :default => 0
|
||||
|
||||
execute %{
|
||||
UPDATE posts
|
||||
SET reshares_count = (
|
||||
SELECT COUNT(*)
|
||||
FROM posts p2
|
||||
WHERE
|
||||
p2.type = 'Reshare'
|
||||
AND p2.root_guid = posts.guid
|
||||
)
|
||||
}
|
||||
if postgres?
|
||||
execute %{
|
||||
UPDATE posts
|
||||
SET reshares_count = (
|
||||
SELECT COUNT(*)
|
||||
FROM posts p2
|
||||
WHERE
|
||||
p2.type = 'Reshare'
|
||||
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
|
||||
|
||||
def self.down
|
||||
|
|
|
|||
Loading…
Reference in a new issue