summaryrefslogtreecommitdiff
path: root/db/post_migrate/20190827102026_migrate_code_owner_approval_status_to_protected_branches_in_batches.rb
blob: b109f582909e67aae5e0b8ca8f2c5cefc4c85b1d (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
# frozen_string_literal: true

class MigrateCodeOwnerApprovalStatusToProtectedBranchesInBatches < ActiveRecord::Migration[5.2]
  include Gitlab::Database::MigrationHelpers

  disable_ddl_transaction!

  DOWNTIME = false
  BATCH_SIZE = 200

  class Project < ActiveRecord::Base
    include EachBatch

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

    has_many :protected_branches
  end

  class ProtectedBranch < ActiveRecord::Base
    include EachBatch

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

    belongs_to :project
  end

  def up
    add_concurrent_index :projects, :id, name: "temp_active_projects_with_prot_branches", where: 'archived = false and pending_delete = false and merge_requests_require_code_owner_approval = true'

    ProtectedBranch
      .joins(:project)
      .where(projects: { archived: false, pending_delete: false, merge_requests_require_code_owner_approval: true })
      .each_batch(of: BATCH_SIZE) do |batch|
        batch.update_all(code_owner_approval_required: true)
      end

    remove_concurrent_index_by_name(:projects, "temp_active_projects_with_prot_branches")
  end

  def down
    # noop
    #
  end
end