Commit 8403991c authored by Shinya Maeda's avatar Shinya Maeda

Fix related environments on pipeline

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

The feature is still behind a feature flag.
parent 447acc66
......@@ -957,7 +957,7 @@ module Ci
.limit(100)
.pluck(:expanded_environment_name)
Environment.where(project: project, name: expanded_environment_names)
Environment.where(project: project, name: expanded_environment_names).with_deployments
else
environment_ids = self_and_descendants.joins(:deployments).select(:'deployments.environment_id')
......
......@@ -89,6 +89,7 @@ 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
......
......@@ -3180,6 +3180,20 @@ RSpec.describe Ci::Pipeline, :mailer, factory_default: :keep do
end
end
context 'when an associated environment does not have deployments' do
let_it_be(:pipeline) { create(:ci_pipeline, :created) }
let_it_be(:build) { create(:ci_build, :stop_review_app, pipeline: pipeline) }
let_it_be(:environment) { create(:environment, project: pipeline.project) }
before_all do
build.metadata.update!(expanded_environment_name: environment.name)
end
it 'does not return environments' do
expect(subject).to be_empty
end
end
context 'when pipeline is in extended family' do
let_it_be(:parent) { create(:ci_pipeline) }
let_it_be(:parent_build) { create(:ci_build, :with_deployment, environment: 'staging', pipeline: parent) }
......
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