Class: Gitlab::BackgroundMigration::BackfillProjectSettings

Inherits:
Object
  • Object
show all
Includes:
Database::DynamicModelHelpers
Defined in:
lib/gitlab/background_migration/backfill_project_settings.rb

Overview

Back-fill project settings for projects that do not yet have one.

Constant Summary

Constants included from Database::DynamicModelHelpers

Database::DynamicModelHelpers::BATCH_SIZE

Instance Method Summary collapse

Methods included from Database::DynamicModelHelpers

#define_batchable_model, #each_batch, #each_batch_range

Instance Method Details

#perform(start_id, end_id, batch_table, batch_column, sub_batch_size, pause_ms) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gitlab/background_migration/backfill_project_settings.rb', line 9

def perform(start_id, end_id, batch_table, batch_column, sub_batch_size, pause_ms)
  batch_relation = relation_scoped_to_range(batch_table, batch_column, start_id, end_id)

  batch_relation.each_batch(column: batch_column, of: sub_batch_size) do |sub_batch|
    insert_sql = <<~SQL
      INSERT INTO project_settings (project_id, created_at, updated_at)
      #{sub_batch.where(project_settings: { project_id: nil })
                 .select('projects.id, NOW(), NOW()')
                 .to_sql}
      ON CONFLICT (project_id) DO NOTHING
    SQL

    connection.execute(insert_sql)

    pause_ms = 0 if pause_ms < 0
    sleep(pause_ms * 0.001)
  end
end