From bf2d54201bb9c2056b4acc6fdeea0bd07c4853ab Mon Sep 17 00:00:00 2001 From: 2458773093 <2458773093@protonmail.com> Date: Mon, 22 Jul 2019 03:46:00 +0300 Subject: [PATCH] group member count optimization --- app/models/group.rb | 1 + app/models/group_account.rb | 10 ++++++++++ app/serializers/rest/group_serializer.rb | 4 ---- .../20190722003541_add_member_count_to_groups.rb | 10 ++++++++++ ...0722003649_backfill_add_member_count_to_groups.rb | 10 ++++++++++ db/schema.rb | 3 ++- lib/tasks/fix_group_member_counts.rake | 12 ++++++++++++ 7 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20190722003541_add_member_count_to_groups.rb create mode 100644 db/migrate/20190722003649_backfill_add_member_count_to_groups.rb create mode 100644 lib/tasks/fix_group_member_counts.rake diff --git a/app/models/group.rb b/app/models/group.rb index 1e3edbfb..1b03b569 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -15,6 +15,7 @@ # is_archived :boolean default(FALSE), not null # created_at :datetime not null # updated_at :datetime not null +# member_count :integer default(0) # class Group < ApplicationRecord diff --git a/app/models/group_account.rb b/app/models/group_account.rb index 12485d98..b5aa62bc 100644 --- a/app/models/group_account.rb +++ b/app/models/group_account.rb @@ -21,10 +21,20 @@ class GroupAccount < ApplicationRecord validates :account_id, uniqueness: { scope: :group_id } after_commit :remove_relationship_cache + after_create :increment_member_count + after_destroy :decrement_member_count private def remove_relationship_cache Rails.cache.delete("relationship:#{account_id}:group#{group_id}") end + + def increment_member_count + group&.increment!(:member_count) + end + + def decrement_member_count + group&.decrement!(:member_count) + end end diff --git a/app/serializers/rest/group_serializer.rb b/app/serializers/rest/group_serializer.rb index 52b1356b..e595bce4 100644 --- a/app/serializers/rest/group_serializer.rb +++ b/app/serializers/rest/group_serializer.rb @@ -22,8 +22,4 @@ class REST::GroupSerializer < ActiveModel::Serializer full_asset_url(object.cover_image.url) end - - def member_count - object.accounts.count - end end diff --git a/db/migrate/20190722003541_add_member_count_to_groups.rb b/db/migrate/20190722003541_add_member_count_to_groups.rb new file mode 100644 index 00000000..9d0193a1 --- /dev/null +++ b/db/migrate/20190722003541_add_member_count_to_groups.rb @@ -0,0 +1,10 @@ +class AddMemberCountToGroups < ActiveRecord::Migration[5.2] + def up + add_column :groups, :member_count, :integer + change_column_default :groups, :member_count, 0 + end + + def down + remove_column :groups, :member_count + end +end diff --git a/db/migrate/20190722003649_backfill_add_member_count_to_groups.rb b/db/migrate/20190722003649_backfill_add_member_count_to_groups.rb new file mode 100644 index 00000000..65a21f6c --- /dev/null +++ b/db/migrate/20190722003649_backfill_add_member_count_to_groups.rb @@ -0,0 +1,10 @@ +class BackfillAddMemberCountToGroups < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + Group.in_batches do |relation| + relation.update_all member_count: 0 + sleep(0.1) + end + end +end diff --git a/db/schema.rb b/db/schema.rb index d9bf6f1e..e074dea8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_07_21_234917) do +ActiveRecord::Schema.define(version: 2019_07_22_003649) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -358,6 +358,7 @@ ActiveRecord::Schema.define(version: 2019_07_21_234917) do t.boolean "is_archived", default: false, null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "member_count", default: 0 t.index ["account_id"], name: "index_groups_on_account_id" end diff --git a/lib/tasks/fix_group_member_counts.rake b/lib/tasks/fix_group_member_counts.rake new file mode 100644 index 00000000..e24ab52b --- /dev/null +++ b/lib/tasks/fix_group_member_counts.rake @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +task fix_group_member_counts: 'gabsocial:fix-group-member-counts' + +namespace :gabsocial do + desc 'Re-compute group member counts' + task :fix_group_member_counts => :environment do + Group.select(:id).all.each do |group| + group.update_column(:member_count, group.accounts.count) + end + end +end