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

mark_duplicate_npm_packages_for_destruction.rb « background_migration « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68cc650d130c6b8b58778c4960f1aac600994f8b (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
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # It seeks duplicate npm packages and mark them for destruction
    class MarkDuplicateNpmPackagesForDestruction < BatchedMigrationJob
      NPM_PACKAGE_TYPE = 2
      PENDING_DESTRUCTION_STATUS = 4

      operation_name :update_all
      feature_category :package_registry

      # Temporary class to link AR model to the `packages_packages` table
      class Package < ::ApplicationRecord
        include EachBatch

        self.table_name = 'packages_packages'
      end

      def perform
        distinct_each_batch do |batch|
          project_ids = batch.pluck(:project_id)

          subquery = Package
            .where(project_id: project_ids, package_type: NPM_PACKAGE_TYPE)
            .where.not(status: PENDING_DESTRUCTION_STATUS)
            .select('project_id, name, version, MAX(id) AS max_id')
            .group(:project_id, :name, :version)
            .having('COUNT(*) > 1')

          join_query = <<~SQL.squish
            INNER JOIN (#{subquery.to_sql}) AS duplicates
            ON packages_packages.project_id = duplicates.project_id
            AND packages_packages.name = duplicates.name
            AND packages_packages.version = duplicates.version
          SQL

          Package
            .joins(join_query)
            .where.not('packages_packages.id = duplicates.max_id')
            .each_batch do |batch_to_update|
            batch_to_update.update_all(status: PENDING_DESTRUCTION_STATUS)
          end
        end
      end
    end
  end
end