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 ...@@ -243,9 +243,9 @@ class Deployment < ApplicationRecord
def previous_deployment def previous_deployment
@previous_deployment ||= @previous_deployment ||=
project.deployments.joins(:environment) self.class.for_environment(environment_id)
.where(environments: { name: self.environment.name }, ref: self.ref) .where(ref: ref)
.where.not(id: self.id) .where.not(id: id)
.order(id: :desc) .order(id: :desc)
.take .take
end end
......
...@@ -574,7 +574,7 @@ RSpec.describe Deployment do ...@@ -574,7 +574,7 @@ RSpec.describe Deployment do
describe '#previous_deployment' do describe '#previous_deployment' do
it 'returns the previous deployment' do it 'returns the previous deployment' do
deploy1 = create(:deployment) deploy1 = create(:deployment, :success)
deploy2 = create( deploy2 = create(
:deployment, :deployment,
project: deploy1.project, project: deploy1.project,
...@@ -583,6 +583,18 @@ RSpec.describe Deployment do ...@@ -583,6 +583,18 @@ RSpec.describe Deployment do
expect(deploy2.previous_deployment).to eq(deploy1) expect(deploy2.previous_deployment).to eq(deploy1)
end 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 end
describe '#link_merge_requests' do 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