From 88c7c1568dd2033f19ca65b7220ebdbb9c5d267a Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sun, 7 Aug 2016 23:30:04 +0200 Subject: [PATCH] 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 --- .../20150404193023_participation_counter.rb | 29 ------------ .../20160807212443_participation_counter.rb | 47 +++++++++++++++++++ db/schema.rb | 2 +- 3 files changed, 48 insertions(+), 30 deletions(-) delete mode 100644 db/migrate/20150404193023_participation_counter.rb create mode 100644 db/migrate/20160807212443_participation_counter.rb diff --git a/db/migrate/20150404193023_participation_counter.rb b/db/migrate/20150404193023_participation_counter.rb deleted file mode 100644 index a13d075b8..000000000 --- a/db/migrate/20150404193023_participation_counter.rb +++ /dev/null @@ -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 diff --git a/db/migrate/20160807212443_participation_counter.rb b/db/migrate/20160807212443_participation_counter.rb new file mode 100644 index 000000000..871a1197f --- /dev/null +++ b/db/migrate/20160807212443_participation_counter.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 50dea294a..90513270a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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