Commit 88c7c156 authored by Benjamin Neff's avatar Benjamin Neff

Rewrite migration for participation counter

This migrations runs only if the old 20150404193023_participation_counter
migration has not been run.

The new migration is a lot faster, because it runs after the
participation-cleanup migration has been run, so the participation table
is now a lot smaller for people upgrading from 0.5.x to 0.6.x.

It also doesn't remove manually created participations (without likes,
comments or poll participations on the same post).

Fixes #6786
parent ca2dd532
class ParticipationCounter < ActiveRecord::Migration
def up
add_column :participations, :count, :int, null: false, default: 1
posts_count = Post.select("COUNT(posts.id)")
.where("posts.id = participations.target_id")
.where("posts.author_id = participations.author_id")
.to_sql
likes_count = Like.select("COUNT(likes.id)")
.where("likes.target_id = participations.target_id")
.where("likes.author_id = participations.author_id")
.to_sql
comments_count = Comment.select("COUNT(comments.id)")
.where("comments.commentable_id = participations.target_id")
.where("comments.author_id = participations.author_id")
.to_sql
polls_count = PollParticipation.select("COUNT(*)")
.where("poll_participations.author_id = participations.author_id")
.joins(:poll)
.where("polls.status_message_id = participations.target_id")
.to_sql
Participation.update_all("count = (#{posts_count}) + (#{likes_count}) + (#{comments_count}) + (#{polls_count})")
Participation.where(count: 0).delete_all
end
def down
remove_column :participations, :count
end
end
class ParticipationCounter < ActiveRecord::Migration
class Comment < ActiveRecord::Base
end
class Like < ActiveRecord::Base
end
class Participation < ActiveRecord::Base
belongs_to :author, class_name: "Person"
end
class Poll < ActiveRecord::Base
end
class PollParticipation < ActiveRecord::Base
belongs_to :poll
end
def up
return if ActiveRecord::SchemaMigration.where(version: "20150404193023").exists?
add_column :participations, :count, :integer, null: false, default: 1
likes_count = Like.select("COUNT(likes.id)")
.where("likes.target_id = participations.target_id")
.where("likes.author_id = participations.author_id")
.to_sql
comments_count = Comment.select("COUNT(comments.id)")
.where("comments.commentable_id = participations.target_id")
.where("comments.author_id = participations.author_id")
.to_sql
polls_count = PollParticipation.select("COUNT(*)")
.where("poll_participations.author_id = participations.author_id")
.joins(:poll)
.where("polls.status_message_id = participations.target_id")
.to_sql
Participation.joins(:author).where.not(people: {owner_id: nil})
.update_all("count = (#{likes_count}) + (#{comments_count}) + (#{polls_count})")
Participation.where(count: 0).update_all(count: 1)
end
def down
remove_column :participations, :count
ActiveRecord::SchemaMigration.where(version: "20150404193023").delete_all
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160802212635) do
ActiveRecord::Schema.define(version: 20160807212443) do
create_table "account_deletions", force: :cascade do |t|
t.string "diaspora_handle", limit: 255
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment