summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/migrate_build_stage_id_reference.rb
blob: 0705b26056dd6e03d3a9f3c3ed856dd5a01feb2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module Gitlab
  module BackgroundMigration
    class MigrateBuildStageIdReference
      def perform(start_id, stop_id)
        scope = if stop_id.nonzero?
                  "ci_builds.id BETWEEN #{start_id.to_i} AND #{stop_id.to_i}"
                else
                  "ci_builds.id >= #{start_id.to_i}"
                end

        sql = <<-SQL.strip_heredoc
          UPDATE "ci_builds"
            SET "stage_id" =
              (SELECT id FROM ci_stages
                WHERE ci_stages.pipeline_id = ci_builds.commit_id
                AND ci_stages.name = ci_builds.stage)
          WHERE #{scope} AND "ci_builds"."stage_id" IS NULL
        SQL

        ActiveRecord::Base.connection.execute(sql)
      end
    end
  end
end