summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/populate_external_pipeline_source.rb
blob: 036fe6417571dafac66acdb73850f86b236b442d (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
49
50
# frozen_string_literal: true
# rubocop:disable Style/Documentation

module Gitlab
  module BackgroundMigration
    class PopulateExternalPipelineSource
      module Migratable
        class Pipeline < ActiveRecord::Base
          self.table_name = 'ci_pipelines'

          def self.sources
            {
              unknown: nil,
              push: 1,
              web: 2,
              trigger: 3,
              schedule: 4,
              api: 5,
              external: 6
            }
          end
        end

        class CommitStatus < ActiveRecord::Base
          self.table_name = 'ci_builds'
          self.inheritance_column = :_type_disabled

          scope :has_pipeline, -> { where('ci_builds.commit_id=ci_pipelines.id') }
          scope :of_type, -> (type) { where('type=?', type) }
        end
      end

      def perform(start_id, stop_id)
        external_pipelines(start_id, stop_id)
          .update_all(source: Migratable::Pipeline.sources[:external])
      end

      private

      def external_pipelines(start_id, stop_id)
        Migratable::Pipeline.where(id: (start_id..stop_id))
          .where(
            'EXISTS (?) AND NOT EXISTS (?)',
            Migratable::CommitStatus.of_type('GenericCommitStatus').has_pipeline.select(1),
            Migratable::CommitStatus.of_type('Ci::Build').has_pipeline.select(1)
          )
      end
    end
  end
end