Commit ac7b5bc9 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'optimise_previous_environment_deployment' into 'master'

Optimise query for Deployments::LinkMergeRequestWorker (Deployment#previous_environment_deployment)

See merge request gitlab-org/gitlab!57039
parents 46d26300 82b17875
......@@ -243,19 +243,16 @@ class Deployment < ApplicationRecord
def previous_deployment
@previous_deployment ||=
project.deployments.joins(:environment)
.where(environments: { name: self.environment.name }, ref: self.ref)
.where.not(id: self.id)
self.class.for_environment(environment_id)
.where(ref: ref)
.where.not(id: id)
.order(id: :desc)
.take
end
def previous_environment_deployment
project
.deployments
self.class.for_environment(environment_id)
.success
.joins(:environment)
.where(environments: { name: environment.name })
.where.not(id: self.id)
.order(id: :desc)
.take
......
---
title: Optimise query for Deployment#previous_environment_deployment in LinkMergeRequestWorker
merge_request: 57039
author:
type: performance
......@@ -574,7 +574,7 @@ RSpec.describe Deployment do
describe '#previous_deployment' do
it 'returns the previous deployment' do
deploy1 = create(:deployment)
deploy1 = create(:deployment, :success)
deploy2 = create(
:deployment,
project: deploy1.project,
......@@ -583,6 +583,18 @@ RSpec.describe Deployment do
expect(deploy2.previous_deployment).to eq(deploy1)
end
it 'returns nothing if the refs do not match' do
deploy1 = create(:deployment, :success)
deploy2 = create(
:deployment,
:review_app,
project: deploy1.project,
environment: deploy1.environment
)
expect(deploy2.previous_deployment).to be_nil
end
end
describe '#link_merge_requests' 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