Class: Gitlab::Database::DeduplicateCiTags

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/database/deduplicate_ci_tags.rb

Constant Summary collapse

TAGGING_BATCH_SIZE =
10_000
TAG_BATCH_SIZE =
10_000
TAGS_INDEX_NAME =
'index_tags_on_name'

Instance Method Summary collapse

Constructor Details

#initialize(logger:, dry_run:) ⇒ DeduplicateCiTags

Returns a new instance of DeduplicateCiTags.



10
11
12
13
# File 'lib/gitlab/database/deduplicate_ci_tags.rb', line 10

def initialize(logger:, dry_run:)
  @logger = logger
  @dry_run = dry_run
end

Instance Method Details

#executeObject



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
# File 'lib/gitlab/database/deduplicate_ci_tags.rb', line 15

def execute
  logger.info "DRY RUN:" if dry_run

  good_tag_ids_query = ::Ci::Tag.group(:name).select('MIN(id) AS id')

  bad_tag_map = ::Ci::Tag
    .id_not_in(good_tag_ids_query)
    .pluck(:id, :name)
    .to_h

  if bad_tag_map.empty?
    logger.info "No duplicate tags found in ci database"
    return
  end

  logger.info "Deduplicating #{bad_tag_map.count} #{'tag'.pluralize(bad_tag_map.count)} for ci database"

  bad_tag_ids = bad_tag_map.keys
  good_tags_name_id_map = ::Ci::Tag.id_in(good_tag_ids_query).pluck(:name, :id).to_h
  tag_remap = bad_tag_map.transform_values { |name| good_tags_name_id_map[name] }

  deduplicate_ci_tags(bad_tag_ids, tag_remap)

  logger.info 'Done'
end