Commit ee0a972b authored by Markus Koller's avatar Markus Koller

Merge branch 'cherry-pick-4c761206' into 'master'

Provide a column to grouped COUNT query

See merge request gitlab-org/gitlab!59897
parents 81bbb111 046b7773
...@@ -46,7 +46,7 @@ module Ci ...@@ -46,7 +46,7 @@ module Ci
self.position = statuses.select(:stage_idx) self.position = statuses.select(:stage_idx)
.where.not(stage_idx: nil) .where.not(stage_idx: nil)
.group(:stage_idx) .group(:stage_idx)
.order('COUNT(*) DESC') .order('COUNT(id) DESC')
.first&.stage_idx.to_i .first&.stage_idx.to_i
end end
......
...@@ -286,6 +286,18 @@ RSpec.describe Ci::Stage, :models do ...@@ -286,6 +286,18 @@ RSpec.describe Ci::Stage, :models do
end end
end end
context 'when stage has statuses with nil idx' do
before do
create(:ci_build, :running, stage_id: stage.id, stage_idx: nil)
create(:ci_build, :running, stage_id: stage.id, stage_idx: 10)
create(:ci_build, :running, stage_id: stage.id, stage_idx: nil)
end
it 'sets index to a non-empty value' do
expect { stage.update_legacy_status }.to change { stage.reload.position }.from(nil).to(10)
end
end
context 'when stage does not have statuses' do context 'when stage does not have statuses' do
it 'fallbacks to zero' do it 'fallbacks to zero' do
expect(stage.reload.position).to be_nil expect(stage.reload.position).to be_nil
......
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