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

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

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

  DOWNTIME = false
  BATCH_SIZE = 10000
  MIGRATION = 'BackfillNamespaceSettings'
  DELAY_INTERVAL = 2.minutes.to_i

  disable_ddl_transaction!

  class Namespace < ActiveRecord::Base
    include EachBatch

    self.table_name = 'namespaces'
  end

  def up
    ensure_data_migration
  end

  def down
    # no-op
  end

  private

  def ensure_data_migration
    Namespace.each_batch(of: BATCH_SIZE) do |query, index|
      missing_count = query.where("NOT EXISTS (SELECT 1 FROM namespace_settings WHERE namespace_settings.namespace_id=namespaces.id)").limit(1).size

      if missing_count > 0
        ids_range = query.pluck("MIN(id), MAX(id)").flatten

        migrate_in(index * DELAY_INTERVAL, MIGRATION, ids_range)
      end
    end
  end
end