group member count optimization
This commit is contained in:
parent
bd3f453c0d
commit
bf2d54201b
@ -15,6 +15,7 @@
|
|||||||
# is_archived :boolean default(FALSE), not null
|
# is_archived :boolean default(FALSE), not null
|
||||||
# created_at :datetime not null
|
# created_at :datetime not null
|
||||||
# updated_at :datetime not null
|
# updated_at :datetime not null
|
||||||
|
# member_count :integer default(0)
|
||||||
#
|
#
|
||||||
|
|
||||||
class Group < ApplicationRecord
|
class Group < ApplicationRecord
|
||||||
|
@ -21,10 +21,20 @@ class GroupAccount < ApplicationRecord
|
|||||||
validates :account_id, uniqueness: { scope: :group_id }
|
validates :account_id, uniqueness: { scope: :group_id }
|
||||||
|
|
||||||
after_commit :remove_relationship_cache
|
after_commit :remove_relationship_cache
|
||||||
|
after_create :increment_member_count
|
||||||
|
after_destroy :decrement_member_count
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def remove_relationship_cache
|
def remove_relationship_cache
|
||||||
Rails.cache.delete("relationship:#{account_id}:group#{group_id}")
|
Rails.cache.delete("relationship:#{account_id}:group#{group_id}")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def increment_member_count
|
||||||
|
group&.increment!(:member_count)
|
||||||
|
end
|
||||||
|
|
||||||
|
def decrement_member_count
|
||||||
|
group&.decrement!(:member_count)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -22,8 +22,4 @@ class REST::GroupSerializer < ActiveModel::Serializer
|
|||||||
|
|
||||||
full_asset_url(object.cover_image.url)
|
full_asset_url(object.cover_image.url)
|
||||||
end
|
end
|
||||||
|
|
||||||
def member_count
|
|
||||||
object.accounts.count
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
10
db/migrate/20190722003541_add_member_count_to_groups.rb
Normal file
10
db/migrate/20190722003541_add_member_count_to_groups.rb
Normal file
@ -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
|
@ -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
|
@ -10,7 +10,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: 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
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
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.boolean "is_archived", default: false, null: false
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_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"
|
t.index ["account_id"], name: "index_groups_on_account_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
12
lib/tasks/fix_group_member_counts.rake
Normal file
12
lib/tasks/fix_group_member_counts.rake
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user