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
This commit is contained in:
parent
ca2dd532af
commit
88c7c1568d
3 changed files with 48 additions and 30 deletions
|
|
@ -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
|
|
||||||
47
db/migrate/20160807212443_participation_counter.rb
Normal file
47
db/migrate/20160807212443_participation_counter.rb
Normal 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
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# 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|
|
create_table "account_deletions", force: :cascade do |t|
|
||||||
t.string "diaspora_handle", limit: 255
|
t.string "diaspora_handle", limit: 255
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue