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

20200608072931_backfill_imported_snippet_repositories.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0566524fa9092ac00f85807dd1b3596c8887f014 (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
# frozen_string_literal: true

class BackfillImportedSnippetRepositories < ActiveRecord::Migration[6.0]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false
  DELAY_INTERVAL = 2.minutes.to_i
  BATCH_SIZE = 200
  MIGRATION = 'BackfillSnippetRepositories'

  disable_ddl_transaction!

  class Snippet < ActiveRecord::Base
    include EachBatch

    self.table_name = 'snippets'
    self.inheritance_column = :_type_disabled
  end

  class SnippetRepository < ActiveRecord::Base
    self.table_name = 'snippet_repositories'
  end

  def up
    index = 1

    Snippet.select(:id).where.not(id: SnippetRepository.select(:snippet_id)).each_batch(of: BATCH_SIZE, column: 'id') do |batch|
      split_in_consecutive_batches(batch).each do |ids_batch|
        migrate_in(index * DELAY_INTERVAL, MIGRATION, [ids_batch.first, ids_batch.last])

        index += 1
      end
    end
  end

  def down
    # no-op
  end

  private

  def split_in_consecutive_batches(relation)
    ids = relation.pluck(:id)

    (ids.first..ids.last).to_a.split {|i| !ids.include?(i) }.select(&:present?)
  end
end