Commit 70714f57 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'fix_counter_cache_invalidation' into 'master'

Fix counter cache invalidation

Closes #32854 and #32870

See merge request !11736
parents 239e08e6 33687db0
...@@ -919,13 +919,13 @@ class User < ActiveRecord::Base ...@@ -919,13 +919,13 @@ class User < ActiveRecord::Base
end end
def assigned_open_merge_requests_count(force: false) def assigned_open_merge_requests_count(force: false)
Rails.cache.fetch(['users', id, 'assigned_open_merge_requests_count'], force: force) do Rails.cache.fetch(['users', id, 'assigned_open_merge_requests_count'], force: force, expires_in: 20.minutes) do
MergeRequestsFinder.new(self, assignee_id: self.id, state: 'opened').execute.count MergeRequestsFinder.new(self, assignee_id: self.id, state: 'opened').execute.count
end end
end end
def assigned_open_issues_count(force: false) def assigned_open_issues_count(force: false)
Rails.cache.fetch(['users', id, 'assigned_open_issues_count'], force: force) do Rails.cache.fetch(['users', id, 'assigned_open_issues_count'], force: force, expires_in: 20.minutes) do
IssuesFinder.new(self, assignee_id: self.id, state: 'opened').execute.count IssuesFinder.new(self, assignee_id: self.id, state: 'opened').execute.count
end end
end end
......
...@@ -28,6 +28,7 @@ module Issues ...@@ -28,6 +28,7 @@ module Issues
notification_service.close_issue(issue, current_user) if notifications notification_service.close_issue(issue, current_user) if notifications
todo_service.close_issue(issue, current_user) todo_service.close_issue(issue, current_user)
execute_hooks(issue, 'close') execute_hooks(issue, 'close')
invalidate_cache_counts(issue.assignees, issue)
end end
issue issue
......
...@@ -8,6 +8,7 @@ module Issues ...@@ -8,6 +8,7 @@ module Issues
create_note(issue) create_note(issue)
notification_service.reopen_issue(issue, current_user) notification_service.reopen_issue(issue, current_user)
execute_hooks(issue, 'reopen') execute_hooks(issue, 'reopen')
invalidate_cache_counts(issue.assignees, issue)
end end
issue issue
......
...@@ -13,6 +13,7 @@ module MergeRequests ...@@ -13,6 +13,7 @@ module MergeRequests
notification_service.close_mr(merge_request, current_user) notification_service.close_mr(merge_request, current_user)
todo_service.close_merge_request(merge_request, current_user) todo_service.close_merge_request(merge_request, current_user)
execute_hooks(merge_request, 'close') execute_hooks(merge_request, 'close')
invalidate_cache_counts(merge_request.assignees, merge_request)
end end
merge_request merge_request
......
...@@ -13,6 +13,7 @@ module MergeRequests ...@@ -13,6 +13,7 @@ module MergeRequests
create_note(merge_request) create_note(merge_request)
notification_service.merge_mr(merge_request, current_user) notification_service.merge_mr(merge_request, current_user)
execute_hooks(merge_request, 'merge') execute_hooks(merge_request, 'merge')
invalidate_cache_counts(merge_request.assignees, merge_request)
end end
private private
......
...@@ -10,6 +10,7 @@ module MergeRequests ...@@ -10,6 +10,7 @@ module MergeRequests
execute_hooks(merge_request, 'reopen') execute_hooks(merge_request, 'reopen')
merge_request.reload_diff(current_user) merge_request.reload_diff(current_user)
merge_request.mark_as_unchecked merge_request.mark_as_unchecked
invalidate_cache_counts(merge_request.assignees, merge_request)
end end
merge_request merge_request
......
...@@ -41,6 +41,12 @@ describe Issues::CloseService, services: true do ...@@ -41,6 +41,12 @@ describe Issues::CloseService, services: true do
service.execute(issue) service.execute(issue)
end end
it 'invalidates counter cache for assignees' do
expect_any_instance_of(User).to receive(:invalidate_issue_cache_counts)
service.execute(issue)
end
end end
describe '#close_issue' do describe '#close_issue' do
......
...@@ -27,6 +27,13 @@ describe Issues::ReopenService, services: true do ...@@ -27,6 +27,13 @@ describe Issues::ReopenService, services: true do
project.team << [user, :master] project.team << [user, :master]
end end
it 'invalidates counter cache for assignees' do
issue.assignees << user
expect_any_instance_of(User).to receive(:invalidate_issue_cache_counts)
described_class.new(project, user).execute(issue)
end
context 'when issue is not confidential' do context 'when issue is not confidential' do
it 'executes issue hooks' do it 'executes issue hooks' do
expect(project).to receive(:execute_hooks).with(an_instance_of(Hash), :issue_hooks) expect(project).to receive(:execute_hooks).with(an_instance_of(Hash), :issue_hooks)
......
...@@ -15,6 +15,8 @@ describe MergeRequests::CloseService, services: true do ...@@ -15,6 +15,8 @@ describe MergeRequests::CloseService, services: true do
end end
describe '#execute' do describe '#execute' do
it_behaves_like 'cache counters invalidator'
context 'valid params' do context 'valid params' do
let(:service) { described_class.new(project, user, {}) } let(:service) { described_class.new(project, user, {}) }
......
require 'spec_helper'
describe MergeRequests::PostMergeService, services: true do
let(:user) { create(:user) }
let(:merge_request) { create(:merge_request, assignee: user) }
let(:project) { merge_request.project }
before do
project.team << [user, :master]
end
describe '#execute' do
it_behaves_like 'cache counters invalidator'
end
end
...@@ -14,6 +14,8 @@ describe MergeRequests::ReopenService, services: true do ...@@ -14,6 +14,8 @@ describe MergeRequests::ReopenService, services: true do
end end
describe '#execute' do describe '#execute' do
it_behaves_like 'cache counters invalidator'
context 'valid params' do context 'valid params' do
let(:service) { described_class.new(project, user, {}) } let(:service) { described_class.new(project, user, {}) }
......
shared_examples 'cache counters invalidator' do
it 'invalidates counter cache for assignees' do
expect_any_instance_of(User).to receive(:invalidate_merge_request_cache_counts)
described_class.new(project, user, {}).execute(merge_request)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment