summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKamil TrzciƄski <ayufan@ayufan.eu>2017-11-21 18:15:51 +0000
committerOswaldo Ferreira <oswaldo@gitlab.com>2017-11-23 01:58:25 +0000
commitc7fcad8e4b10bc0a09b0c5ae954a5f856bed3cbe (patch)
tree42527864bbbbf574a76644e6412825e7b75b1652
parentb5343c3065da52550ee4893cc82c00c4af85dda1 (diff)
downloadgitlab-ce-c7fcad8e4b10bc0a09b0c5ae954a5f856bed3cbe.tar.gz
Merge branch 'reduce-queries-for-artifacts-button' into 'master'
Use arrays in Ci::Pipeline#latest_builds_with_artifacts See merge request gitlab-org/gitlab-ce!15525 (cherry picked from commit ce019812a5127d503c735f5fe870e2aa4ad14665) 54f1e406 Use arrays in Pipeline#latest_builds_with_artifacts
-rw-r--r--app/models/ci/pipeline.rb5
-rw-r--r--changelogs/unreleased/reduce-queries-for-artifacts-button.yml5
-rw-r--r--spec/models/ci/pipeline_spec.rb4
3 files changed, 13 insertions, 1 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb
index 797a12d4e63..ec12dd7c210 100644
--- a/app/models/ci/pipeline.rb
+++ b/app/models/ci/pipeline.rb
@@ -505,7 +505,10 @@ module Ci
end
def latest_builds_with_artifacts
- @latest_builds_with_artifacts ||= builds.latest.with_artifacts
+ # We purposely cast the builds to an Array here. Because we always use the
+ # rows if there are more than 0 this prevents us from having to run two
+ # queries: one to get the count and one to get the rows.
+ @latest_builds_with_artifacts ||= builds.latest.with_artifacts.to_a
end
private
diff --git a/changelogs/unreleased/reduce-queries-for-artifacts-button.yml b/changelogs/unreleased/reduce-queries-for-artifacts-button.yml
new file mode 100644
index 00000000000..f2d469b5a80
--- /dev/null
+++ b/changelogs/unreleased/reduce-queries-for-artifacts-button.yml
@@ -0,0 +1,5 @@
+---
+title: Use arrays in Pipeline#latest_builds_with_artifacts
+merge_request:
+author:
+type: performance
diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb
index b89b0e555d9..3a19a0753e2 100644
--- a/spec/models/ci/pipeline_spec.rb
+++ b/spec/models/ci/pipeline_spec.rb
@@ -1502,6 +1502,10 @@ describe Ci::Pipeline, :mailer do
create(:ci_build, :success, :artifacts, pipeline: pipeline)
end
+ it 'returns an Array' do
+ expect(pipeline.latest_builds_with_artifacts).to be_an_instance_of(Array)
+ end
+
it 'returns the latest builds' do
expect(pipeline.latest_builds_with_artifacts).to eq([build])
end