From 5771972e8ca2a7ad06445ee539054b23cf228de8 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 24 Apr 2018 11:18:12 +0200 Subject: Use database query to calculate average stage position --- app/models/ci/stage.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'app/models/ci/stage.rb') diff --git a/app/models/ci/stage.rb b/app/models/ci/stage.rb index a80c72d3452..5a77a909b9d 100644 --- a/app/models/ci/stage.rb +++ b/app/models/ci/stage.rb @@ -24,12 +24,14 @@ module Ci self.status = DEFAULT_STATUS if self.status.nil? end - before_validation do - next unless index.nil? + before_validation unless: :importing? do + next if index.present? - statuses.pluck(:stage_idx).tap do |indices| - self.index = indices.max_by { |index| indices.count(index) } - end + self.index = statuses.select(:stage_idx) + .where('stage_idx IS NOT NULL') + .group(:stage_idx) + .order('COUNT(*) DESC') + .first&.stage_idx.to_i end state_machine :status, initial: :created do -- cgit v1.2.1