Welcome to mirror list, hosted at ThFree Co, Russian Federation.

populate_has_vulnerabilities.rb « background_migration « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 78140b768fc1427820e2a64b990c867583c636f2 (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
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # This class populates missing dismissal information for
    # vulnerability entries.
    class PopulateHasVulnerabilities
      class ProjectSetting < ActiveRecord::Base # rubocop:disable Style/Documentation
        self.table_name = 'project_settings'

        UPSERT_SQL = <<~SQL
          WITH upsert_data (project_id, has_vulnerabilities, created_at, updated_at) AS (
            SELECT projects.id, true, current_timestamp, current_timestamp FROM projects WHERE projects.id IN (%{project_ids})
          )
          INSERT INTO project_settings
          (project_id, has_vulnerabilities, created_at, updated_at)
          (SELECT * FROM upsert_data)
          ON CONFLICT (project_id)
          DO UPDATE SET
            has_vulnerabilities = true,
            updated_at = EXCLUDED.updated_at
        SQL

        def self.upsert_for(project_ids)
          connection.execute(UPSERT_SQL % { project_ids: project_ids.join(', ') })
        end
      end

      class Vulnerability < ActiveRecord::Base # rubocop:disable Style/Documentation
        include EachBatch

        self.table_name = 'vulnerabilities'
      end

      def perform(*project_ids)
        ProjectSetting.upsert_for(project_ids)
      rescue StandardError => e
        log_error(e, project_ids)
      ensure
        log_info(project_ids)
      end

      private

      def log_error(error, project_ids)
        ::Gitlab::BackgroundMigration::Logger.error(
          migrator: self.class.name,
          message: error.message,
          project_ids: project_ids
        )
      end

      def log_info(project_ids)
        ::Gitlab::BackgroundMigration::Logger.info(
          migrator: self.class.name,
          message: 'Projects has been processed to populate `has_vulnerabilities` information',
          count: project_ids.length
        )
      end
    end
  end
end