Class: Gitlab::BackgroundMigration::BackfillProjectIdToDependencyListExports

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

Defined Under Namespace

Classes: DependencyListExport, Pipeline

Constant Summary

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

#bulk_update!(tuples) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gitlab/background_migration/backfill_project_id_to_dependency_list_exports.rb', line 46

def bulk_update!(tuples)
  return if tuples.blank?

  values_sql = Arel::Nodes::ValuesList.new(tuples).to_sql

  sql = <<~SQL
    UPDATE
      dependency_list_exports
    SET
      project_id = tuples.project_id
    FROM
      (#{values_sql}) AS tuples(export_id, project_id)
    WHERE
      dependency_list_exports.id = tuples.export_id;
  SQL

  DependencyListExport.connection.execute(sql)
end

#completed?(export) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/gitlab/background_migration/backfill_project_id_to_dependency_list_exports.rb', line 65

def completed?(export)
  export.status.in?([DependencyListExport::FINISHED, DependencyListExport::FAILED])
end

#dangling?(export) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/gitlab/background_migration/backfill_project_id_to_dependency_list_exports.rb', line 75

def dangling?(export)
  completed?(export) && stale?(export)
end

#performObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gitlab/background_migration/backfill_project_id_to_dependency_list_exports.rb', line 24

def perform
  each_sub_batch do |exports|
    pipelines = Pipeline.id_in(exports.map(&:pipeline_id))

    export_ids_to_delete = []

    tuples_to_update = exports.filter_map do |export|
      pipeline = pipelines.find { |pipeline| pipeline.id == export.pipeline_id }

      if pipeline.blank? || dangling?(export)
        export_ids_to_delete.push(export.id)
        next
      end

      [export.id, pipeline.project_id] if export.project_id != pipeline.project_id
    end

    DependencyListExport.id_in(export_ids_to_delete).delete_all
    bulk_update!(tuples_to_update)
  end
end

#stale?(export) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
# File 'lib/gitlab/background_migration/backfill_project_id_to_dependency_list_exports.rb', line 69

def stale?(export)
  # We delete exports one hour after completion and runtime
  # is upwards of 30 mins.
  export.updated_at < 3.hours.ago
end