summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/migrate_stage_index.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/background_migration/migrate_stage_index.rb')
-rw-r--r--lib/gitlab/background_migration/migrate_stage_index.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/gitlab/background_migration/migrate_stage_index.rb b/lib/gitlab/background_migration/migrate_stage_index.rb
new file mode 100644
index 00000000000..e66fec7b20d
--- /dev/null
+++ b/lib/gitlab/background_migration/migrate_stage_index.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+# rubocop:disable Style/Documentation
+
+module Gitlab
+ module BackgroundMigration
+ class MigrateStageIndex
+ module Migratable
+ class Stage < ActiveRecord::Base
+ self.table_name = 'ci_stages'
+ end
+ end
+
+ def perform(start_id, stop_id)
+ sql = <<~SQL
+ UPDATE ci_stages
+ SET index =
+ (SELECT stage_idx FROM ci_builds
+ WHERE ci_builds.stage_id = ci_stages.id
+ GROUP BY ci_builds.stage_idx ORDER BY COUNT(*) DESC LIMIT 1)
+ WHERE ci_stages.id BETWEEN #{start_id.to_i} AND #{stop_id.to_i}
+ SQL
+
+ ActiveRecord::Base.connection.execute(sql)
+ end
+ end
+ end
+end