summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/cleanup_draft_data_from_faulty_regex.rb
blob: b703faf6a6cdea39b5cf693eca8596477e365907 (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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # Cleanup draft column data inserted by a faulty regex
    #
    class CleanupDraftDataFromFaultyRegex
      # Migration only version of MergeRequest table
      ##
      class MergeRequest < ActiveRecord::Base
        LEAKY_REGEXP_STR     = "^\\[draft\\]|\\(draft\\)|draft:|draft|\\[WIP\\]|WIP:|WIP"
        CORRECTED_REGEXP_STR = "^(\\[draft\\]|\\(draft\\)|draft:|draft|\\[WIP\\]|WIP:|WIP)"

        include EachBatch

        self.table_name = 'merge_requests'

        def self.eligible
          where(state_id: 1)
            .where(draft: true)
            .where("title ~* ?", LEAKY_REGEXP_STR)
            .where("title !~* ?", CORRECTED_REGEXP_STR)
        end
      end

      def perform(start_id, end_id)
        eligible_mrs = MergeRequest.eligible.where(id: start_id..end_id).pluck(:id)

        return if eligible_mrs.empty?

        eligible_mrs.each_slice(10) do |slice|
          MergeRequest.where(id: slice).update_all(draft: false)
        end

        mark_job_as_succeeded(start_id, end_id)
      end

      private

      def mark_job_as_succeeded(*arguments)
        Gitlab::Database::BackgroundMigrationJob.mark_all_as_succeeded(
          'CleanupDraftDataFromFaultyRegex',
          arguments
        )
      end
    end
  end
end