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

20210629031900_associate_existing_dast_builds_with_variables.rb « migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3d81db87143dba6fb607be9983e9a2d3c0c372d8 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true

class AssociateExistingDastBuildsWithVariables < ActiveRecord::Migration[6.1]
  disable_ddl_transaction!

  class Profile < ApplicationRecord
    self.table_name = 'dast_profiles'
    self.inheritance_column = :_type_disabled
  end

  class ProfilesPipeline < ApplicationRecord
    include EachBatch

    self.table_name = 'dast_profiles_pipelines'
    self.inheritance_column = :_type_disabled

    belongs_to :profile, foreign_key: :dast_profile_id
  end

  class Build < ApplicationRecord
    self.table_name = 'ci_builds'
    self.inheritance_column = :_type_disabled
    self.gitlab_schema = :gitlab_ci

    default_scope { where(name: :dast, stage: :dast) } # rubocop:disable Cop/DefaultScope
  end

  class SiteProfilesBuild < ApplicationRecord
    self.table_name = 'dast_site_profiles_builds'
    self.inheritance_column = :_type_disabled
  end

  BATCH_SIZE = 300

  def up
    process_batch do |batch|
      bulk_inserts = []

      grouped_builds = fetch_builds(batch).group_by(&:commit_id)

      batch.includes(:profile).each do |profile_pipeline|
        builds = grouped_builds[profile_pipeline.ci_pipeline_id]

        next if builds.blank?

        builds.each do |build|
          bulk_inserts.push(dast_site_profile_id: profile_pipeline.profile.dast_site_profile_id, ci_build_id: build.id)
        end
      end

      SiteProfilesBuild.insert_all(bulk_inserts, unique_by: :ci_build_id)
    end
  end

  def down
    process_batch do |batch|
      builds = fetch_builds(batch)

      SiteProfilesBuild
        .where(ci_build_id: builds)
        .delete_all
    end
  end

  private

  def fetch_builds(batch)
    # pluck necessary to support ci table decomposition
    # https://gitlab.com/groups/gitlab-org/-/epics/6289
    Build.where(commit_id: batch.pluck(:ci_pipeline_id))
  end

  def process_batch
    ProfilesPipeline.each_batch(of: BATCH_SIZE, column: :ci_pipeline_id) do |batch|
      yield(batch)
    end
  end
end