Welcome to mirror list, hosted at ThFree Co, Russian Federation.

destroy_label_links_service.rb « issuable « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6fff9b5e8d299e9ec5c85df3909724ca7345618c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true

module Issuable
  class DestroyLabelLinksService
    BATCH_SIZE = 100

    def initialize(target_id, target_type)
      @target_id = target_id
      @target_type = target_type
    end

    def execute
      inner_query =
        LabelLink
          .select(:id)
          .for_target(target_id, target_type)
          .limit(BATCH_SIZE)

      delete_query = <<~SQL
      DELETE FROM "#{LabelLink.table_name}"
      WHERE id IN (#{inner_query.to_sql})
      SQL

      loop do
        result = ActiveRecord::Base.connection.execute(delete_query)

        break if result.cmd_tuples == 0
      end
    end

    private

    attr_reader :target_id, :target_type
  end
end