Commit 0f936592 authored by Igor Drozdov's avatar Igor Drozdov

Scope pipelines to a project while loading artifacts

This commit improves performance of a query
parent fd486c93
......@@ -74,7 +74,7 @@ module Ci
scope :with_files_stored_locally, -> { where(file_store: [nil, ::JobArtifactUploader::Store::LOCAL]) }
scope :with_files_stored_remotely, -> { where(file_store: ::JobArtifactUploader::Store::REMOTE) }
scope :for_sha, ->(sha) { joins(job: :pipeline).where(ci_pipelines: { sha: sha }) }
scope :for_sha, ->(sha, project_id) { joins(job: :pipeline).where(ci_pipelines: { sha: sha, project_id: project_id }) }
scope :with_file_types, -> (file_types) do
types = self.file_types.select { |file_type| file_types.include?(file_type) }.values
......
......@@ -21,9 +21,9 @@ module API
authorize! :download_code, user_project
artifact =
@project.job_artifacts
Ci::JobArtifact
.with_file_types(['lsif'])
.for_sha(params[:commit_id])
.for_sha(params[:commit_id], @project.id)
.last
not_found! unless artifact
......
......@@ -113,13 +113,14 @@ describe Ci::JobArtifact do
describe '.for_sha' do
it 'returns job artifacts for a given pipeline sha' do
first_pipeline = create(:ci_pipeline)
second_pipeline = create(:ci_pipeline, sha: Digest::SHA1.hexdigest(SecureRandom.hex))
project = create(:project)
first_pipeline = create(:ci_pipeline, project: project)
second_pipeline = create(:ci_pipeline, project: project, sha: Digest::SHA1.hexdigest(SecureRandom.hex))
first_artifact = create(:ci_job_artifact, job: create(:ci_build, pipeline: first_pipeline))
second_artifact = create(:ci_job_artifact, job: create(:ci_build, pipeline: second_pipeline))
expect(described_class.for_sha(first_pipeline.sha)).to eq([first_artifact])
expect(described_class.for_sha(second_pipeline.sha)).to eq([second_artifact])
expect(described_class.for_sha(first_pipeline.sha, project.id)).to eq([first_artifact])
expect(described_class.for_sha(second_pipeline.sha, project.id)).to eq([second_artifact])
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