summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-04-20 12:44:20 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-04-24 11:07:37 +0200
commit02999234d4b7ebcf60332816b68addf8f4667ce7 (patch)
tree3e5f60144e54cf18c49192a68e7431cd0525deaa /lib
parent3b04a3dc4ef9c9cd48d2706736d9fcd9903a5dd0 (diff)
downloadgitlab-ce-02999234d4b7ebcf60332816b68addf8f4667ce7.tar.gz
Add background migration that migrates stages indices
Diffstat (limited to 'lib')
-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