Class: Gitlab::BackgroundMigration::RecalculateShardingKeyIdForOrphanedProjectRunners

Inherits:
BatchedMigrationJob
  • Object
show all
Defined in:
lib/gitlab/background_migration/recalculate_sharding_key_id_for_orphaned_project_runners.rb

Defined Under Namespace

Classes: CiRunner, CiRunnerProject

Constant Summary

Constants inherited from BatchedMigrationJob

BatchedMigrationJob::DEFAULT_FEATURE_CATEGORY, BatchedMigrationJob::MINIMUM_PAUSE_MS

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gitlab/background_migration/recalculate_sharding_key_id_for_orphaned_project_runners.rb', line 20

def perform
  runner_projects = CiRunnerProject.where("#{CiRunnerProject.table_name}.runner_id = #{CiRunner.table_name}.id")

  each_sub_batch do |sub_batch|
    runners_missing_owner_project =
      CiRunner.id_in(sub_batch.pluck(:id))
        .where_not_exists( # With a missing project connection
          runner_projects
            .where("#{CiRunnerProject.table_name}.project_id = #{CiRunner.table_name}.sharding_key_id")
            .limit(1)
        )
    # But with a fallback project connection
    runners_with_fallback_owner = runners_missing_owner_project.where_exists(runner_projects.limit(1))

    runners_with_fallback_owner.update_all <<~SQL
      sharding_key_id = (#{runner_projects.order(id: :asc).limit(1).select(:project_id).to_sql})
    SQL

    # Delete orphaned runners, cascading to runner managers, and runner taggings
    runners_missing_owner_project.delete_all
  end
end