Commit 046b7773 authored by Igor Drozdov's avatar Igor Drozdov

Provide a column to grouped COUNT query

That fixes Rails warning about dangerous query method
parent 2376a5ee
......@@ -46,7 +46,7 @@ module Ci
self.position = statuses.select(:stage_idx)
.where.not(stage_idx: nil)
.group(:stage_idx)
.order('COUNT(*) DESC')
.order('COUNT(id) DESC')
.first&.stage_idx.to_i
end
......
......@@ -286,6 +286,18 @@ RSpec.describe Ci::Stage, :models do
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
it 'fallbacks to zero' do
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