Merge pull request #6946 from SuperTux88/6786-fix-participation-counter-migration

Rewrite migration for participation counter
This commit is contained in:
Jonne Haß 2016-08-08 21:59:16 +02:00 committed by GitHub
commit 5c62af2821
3 changed files with 48 additions and 30 deletions

View file

@ -1,29 +0,0 @@
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

View file

@ -0,0 +1,47 @@
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

View file

@ -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