Class: Gitlab::BackgroundMigration::BackfillShardingKeyIdOnCiRunners

Inherits:
BatchedMigrationJob show all
Defined in:
lib/gitlab/background_migration/backfill_sharding_key_id_on_ci_runners.rb

Defined Under Namespace

Classes: CiRunner

Constant Summary collapse

UPDATE_GROUP_RUNNERS_SQL =
<<~SQL
  UPDATE ci_runners
  SET sharding_key_id = ci_runner_namespaces.namespace_id
  FROM ci_runner_namespaces
  WHERE ci_runners.id = ci_runner_namespaces.runner_id
    AND ci_runners.id IN (?);
SQL
UPDATE_PROJECT_RUNNERS_SQL =
<<~SQL
  UPDATE ci_runners
  SET sharding_key_id = (
    SELECT ci_runner_projects.project_id
    FROM ci_runner_projects
    WHERE ci_runner_projects.runner_id = ci_runners.id
    ORDER BY ci_runner_projects.id ASC
    LIMIT 1
  )
  FROM ci_runner_projects
  WHERE ci_runners.id = ci_runner_projects.runner_id
    AND ci_runners.id IN (?);
SQL

Constants inherited from BatchedMigrationJob

Gitlab::BackgroundMigration::BatchedMigrationJob::DEFAULT_FEATURE_CATEGORY

Constants included from Database::DynamicModelHelpers

Database::DynamicModelHelpers::BATCH_SIZE

Instance Method Summary collapse

Methods inherited from BatchedMigrationJob

#batch_metrics, cursor, cursor?, cursor_columns, feature_category, #filter_batch, generic_instance, #initialize, job_arguments, job_arguments_count, operation_name, scope_to

Methods included from Database::DynamicModelHelpers

#define_batchable_model, #each_batch, #each_batch_range

Constructor Details

This class inherits a constructor from Gitlab::BackgroundMigration::BatchedMigrationJob

Instance Method Details

#performObject



37
38
39
40
41
42
43
44
# File 'lib/gitlab/background_migration/backfill_sharding_key_id_on_ci_runners.rb', line 37

def perform
  each_sub_batch do |sub_batch|
    sub_batch = sub_batch.where(sharding_key_id: nil).limit(sub_batch_size).select(:id)

    connection.exec_update(CiRunner.sanitize_sql([UPDATE_GROUP_RUNNERS_SQL, sub_batch.where(runner_type: 2)]))
    connection.exec_update(CiRunner.sanitize_sql([UPDATE_PROJECT_RUNNERS_SQL, sub_batch.where(runner_type: 3)]))
  end
end