Commit 82b17875 authored by Thong Kuah's avatar Thong Kuah

Optimise query for previous_deployment

- No need to search via environment.name as environments must have
unique name per project.
parent 15bfd724
......@@ -243,9 +243,9 @@ 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
......
......@@ -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