summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/destroy_invalid_group_members.rb
blob: 79aae719d03cd156af9daa10c8403cc7f0efcdc8 (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
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    class DestroyInvalidGroupMembers < Gitlab::BackgroundMigration::BatchedMigrationJob # rubocop:disable Style/Documentation
      scope_to ->(relation) do
        relation.where(source_type: 'Namespace')
                .joins('LEFT OUTER JOIN namespaces ON members.source_id = namespaces.id')
                .where(namespaces: { id: nil })
      end

      operation_name :delete_all
      feature_category :database

      def perform
        each_sub_batch do |sub_batch|
          invalid_ids = sub_batch.map(&:id)
          Gitlab::AppLogger.info({ message: 'Removing invalid group member records',
                                   deleted_count: invalid_ids.size, ids: invalid_ids })

          sub_batch.delete_all
        end
      end
    end
  end
end