Commit 85dcd0a9 authored by Shinya Maeda's avatar Shinya Maeda

Fix environments query for pipeline

This commit fixes the database query to fetch
related environments of a pipeline.

The feature is still behind a feature flag.
parent 09a4bb5b
......@@ -958,7 +958,7 @@ module Ci
.limit(100)
.pluck(:expanded_environment_name)
Environment.where(project: project, name: expanded_environment_names).with_deployments
Environment.where(project: project, name: expanded_environment_names).with_deployment(sha)
else
environment_ids = self_and_descendants.joins(:deployments).select(:'deployments.environment_id')
......
......@@ -89,7 +89,6 @@ class Environment < ApplicationRecord
scope :for_project, -> (project) { where(project_id: project) }
scope :for_tier, -> (tier) { where(tier: tier).where.not(tier: nil) }
scope :with_deployments, -> { where('EXISTS (?)', Deployment.select(1).where('deployments.environment_id = environments.id')) }
scope :with_deployment, -> (sha) { where('EXISTS (?)', Deployment.select(1).where('deployments.environment_id = environments.id').where(sha: sha)) }
scope :unfoldered, -> { where(environment_type: nil) }
scope :with_rank, -> do
......
......@@ -3205,11 +3205,21 @@ RSpec.describe Ci::Pipeline, :mailer, factory_default: :keep do
context 'when pipeline is not child nor parent' do
let_it_be(:pipeline) { create(:ci_pipeline, :created) }
let_it_be(:build) { create(:ci_build, :with_deployment, :deploy_to_production, pipeline: pipeline) }
let_it_be(:build, refind: true) { create(:ci_build, :with_deployment, :deploy_to_production, pipeline: pipeline) }
it 'returns just the pipeline environment' do
expect(subject).to contain_exactly(build.deployment.environment)
end
context 'when deployment SHA is not matched' do
before do
build.deployment.update!(sha: 'old-sha')
end
it 'does not return environments' do
expect(subject).to be_empty
end
end
end
context 'when an associated environment does not have deployments' do
......
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