summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToon Claes <toon@gitlab.com>2017-04-21 11:36:34 +0200
committerToon Claes <toon@gitlab.com>2017-05-17 15:05:48 +0200
commit9c02244cafb7ac70fe9f7f01b86894d00d57bf29 (patch)
treea890eea50e8f2c9a8b8022a0c7c2a26ef978bddc
parent3d44ec3f4aa7180f2226f09064abe281c6b9d7be (diff)
downloadgitlab-ce-tc-no-todo-service-select-mysql.tar.gz
Avoid plucking Todo ids and use sub-queries insteadtc-no-todo-service-select-mysql
TodoService should not call `.select(&:id)` on todos, because this is bad performance. So instead use sub-queries, which will result in a single SQL query to the database. https://docs.gitlab.com/ee/development/sql.html#plucking-ids
-rw-r--r--app/controllers/dashboard/todos_controller.rb4
-rw-r--r--app/services/issuable_base_service.rb2
-rw-r--r--app/services/todo_service.rb16
-rw-r--r--changelogs/unreleased/tc-no-todo-service-select.yml4
-rw-r--r--lib/api/todos.rb3
-rw-r--r--lib/api/v3/todos.rb3
-rw-r--r--spec/services/todo_service_spec.rb36
7 files changed, 44 insertions, 24 deletions
diff --git a/app/controllers/dashboard/todos_controller.rb b/app/controllers/dashboard/todos_controller.rb
index 4d7d45787fc..2b76e57c06a 100644
--- a/app/controllers/dashboard/todos_controller.rb
+++ b/app/controllers/dashboard/todos_controller.rb
@@ -12,7 +12,7 @@ class Dashboard::TodosController < Dashboard::ApplicationController
end
def destroy
- TodoService.new.mark_todos_as_done_by_ids([params[:id]], current_user)
+ TodoService.new.mark_todos_as_done_by_ids(params[:id], current_user)
respond_to do |format|
format.html { redirect_to dashboard_todos_path, notice: 'Todo was successfully marked as done.' }
@@ -32,7 +32,7 @@ class Dashboard::TodosController < Dashboard::ApplicationController
end
def restore
- TodoService.new.mark_todos_as_pending_by_ids([params[:id]], current_user)
+ TodoService.new.mark_todos_as_pending_by_ids(params[:id], current_user)
render json: todos_counts
end
diff --git a/app/services/issuable_base_service.rb b/app/services/issuable_base_service.rb
index dc2ab99b982..143de32efdf 100644
--- a/app/services/issuable_base_service.rb
+++ b/app/services/issuable_base_service.rb
@@ -281,7 +281,7 @@ class IssuableBaseService < BaseService
todo_service.mark_todo(issuable, current_user)
when 'done'
todo = TodosFinder.new(current_user).execute.find_by(target: issuable)
- todo_service.mark_todos_as_done([todo], current_user) if todo
+ todo_service.mark_todos_as_done_by_ids(todo, current_user) if todo
end
end
diff --git a/app/services/todo_service.rb b/app/services/todo_service.rb
index 322c6286365..6ee96d6a0f8 100644
--- a/app/services/todo_service.rb
+++ b/app/services/todo_service.rb
@@ -170,20 +170,22 @@ class TodoService
# When user marks some todos as done
def mark_todos_as_done(todos, current_user)
- update_todos_state_by_ids(todos.select(&:id), current_user, :done)
+ update_todos_state(todos, current_user, :done)
end
def mark_todos_as_done_by_ids(ids, current_user)
- update_todos_state_by_ids(ids, current_user, :done)
+ todos = todos_by_ids(ids, current_user)
+ mark_todos_as_done(todos, current_user)
end
# When user marks some todos as pending
def mark_todos_as_pending(todos, current_user)
- update_todos_state_by_ids(todos.select(&:id), current_user, :pending)
+ update_todos_state(todos, current_user, :pending)
end
def mark_todos_as_pending_by_ids(ids, current_user)
- update_todos_state_by_ids(ids, current_user, :pending)
+ todos = todos_by_ids(ids, current_user)
+ mark_todos_as_pending(todos, current_user)
end
# When user marks an issue as todo
@@ -198,9 +200,11 @@ class TodoService
private
- def update_todos_state_by_ids(ids, current_user, state)
- todos = current_user.todos.where(id: ids)
+ def todos_by_ids(ids, current_user)
+ current_user.todos.where(id: Array(ids))
+ end
+ def update_todos_state(todos, current_user, state)
# Only update those that are not really on that state
todos = todos.where.not(state: state)
todos_ids = todos.pluck(:id)
diff --git a/changelogs/unreleased/tc-no-todo-service-select.yml b/changelogs/unreleased/tc-no-todo-service-select.yml
new file mode 100644
index 00000000000..ddcae334aa7
--- /dev/null
+++ b/changelogs/unreleased/tc-no-todo-service-select.yml
@@ -0,0 +1,4 @@
+---
+title: Avoid plucking Todo ids in TodoService
+merge_request: 10845
+author:
diff --git a/lib/api/todos.rb b/lib/api/todos.rb
index d1f7e364029..b9c3434fee1 100644
--- a/lib/api/todos.rb
+++ b/lib/api/todos.rb
@@ -59,8 +59,7 @@ module API
requires :id, type: Integer, desc: 'The ID of the todo being marked as done'
end
post ':id/mark_as_done' do
- todo = current_user.todos.find(params[:id])
- TodoService.new.mark_todos_as_done([todo], current_user)
+ TodoService.new.mark_todos_as_done_by_ids(params[:id], current_user)
present todo.reload, with: Entities::Todo, current_user: current_user
end
diff --git a/lib/api/v3/todos.rb b/lib/api/v3/todos.rb
index e3b311d61cd..1bda0775904 100644
--- a/lib/api/v3/todos.rb
+++ b/lib/api/v3/todos.rb
@@ -11,8 +11,7 @@ module API
requires :id, type: Integer, desc: 'The ID of the todo being marked as done'
end
delete ':id' do
- todo = current_user.todos.find(params[:id])
- TodoService.new.mark_todos_as_done([todo], current_user)
+ TodoService.new.mark_todos_as_done_by_ids(params[:id], current_user)
present todo.reload, with: ::API::Entities::Todo, current_user: current_user
end
diff --git a/spec/services/todo_service_spec.rb b/spec/services/todo_service_spec.rb
index 175a42a32d9..c3d7d7243b6 100644
--- a/spec/services/todo_service_spec.rb
+++ b/spec/services/todo_service_spec.rb
@@ -336,7 +336,7 @@ describe TodoService, services: true do
describe '#mark_todos_as_done' do
it_behaves_like 'updating todos state', :mark_todos_as_done, :pending, :done do
- let(:collection) { [first_todo, second_todo] }
+ let(:collection) { Todo.all }
end
end
@@ -348,7 +348,7 @@ describe TodoService, services: true do
describe '#mark_todos_as_pending' do
it_behaves_like 'updating todos state', :mark_todos_as_pending, :done, :pending do
- let(:collection) { [first_todo, second_todo] }
+ let(:collection) { Todo.all }
end
end
@@ -877,17 +877,11 @@ describe TodoService, services: true do
.to change { john_doe.todos.done.count }.from(0).to(1)
end
- it 'marks an array of todos as done' do
- todo = create(:todo, :mentioned, user: john_doe, target: issue, project: project)
-
- expect { TodoService.new.mark_todos_as_done([todo], john_doe) }
- .to change { todo.reload.state }.from('pending').to('done')
- end
-
it 'returns the ids of updated todos' do # Needed on API
todo = create(:todo, :mentioned, user: john_doe, target: issue, project: project)
- expect(TodoService.new.mark_todos_as_done([todo], john_doe)).to eq([todo.id])
+ todos = TodosFinder.new(john_doe, {}).execute
+ expect(TodoService.new.mark_todos_as_done(todos, john_doe)).to eq([todo.id])
end
context 'when some of the todos are done already' do
@@ -907,11 +901,31 @@ describe TodoService, services: true do
expect(TodoService.new.mark_todos_as_done(Todo.all, john_doe)).to eq([])
end
end
+ end
+
+ describe '#mark_todos_as_done_by_ids' do
+ let(:issue) { create(:issue, project: project, author: author, assignees: [john_doe]) }
+ let(:another_issue) { create(:issue, project: project, author: author, assignees: [john_doe]) }
+
+ it 'marks an array of todo ids as done' do
+ todo = create(:todo, :mentioned, user: john_doe, target: issue, project: project)
+ another_todo = create(:todo, :mentioned, user: john_doe, target: another_issue, project: project)
+
+ expect { TodoService.new.mark_todos_as_done_by_ids([todo.id, another_todo.id], john_doe) }
+ .to change { john_doe.todos.done.count }.from(0).to(2)
+ end
+
+ it 'marks a single todo id as done' do
+ todo = create(:todo, :mentioned, user: john_doe, target: issue, project: project)
+
+ expect { TodoService.new.mark_todos_as_done_by_ids(todo.id, john_doe) }
+ .to change { todo.reload.state }.from('pending').to('done')
+ end
it 'caches the number of todos of a user', :caching do
create(:todo, :mentioned, user: john_doe, target: issue, project: project)
todo = create(:todo, :mentioned, user: john_doe, target: issue, project: project)
- TodoService.new.mark_todos_as_done([todo], john_doe)
+ TodoService.new.mark_todos_as_done_by_ids(todo, john_doe)
expect_any_instance_of(TodosFinder).not_to receive(:execute)