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

backfill_packages_tags_project_id.rb « background_migration « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 04fd09f81f02f702c6cc502fa328201e0587a95a (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
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # This migration populates the new `packages_tags.project_id` column from joining with `packages_packages` table
    class BackfillPackagesTagsProjectId < BatchedMigrationJob
      operation_name :update_all # This is used as the key on collecting metrics
      scope_to ->(relation) { relation.where(project_id: nil) }
      feature_category :package_registry

      def perform
        each_sub_batch do |sub_batch|
          joined = sub_batch
            .joins('INNER JOIN packages_packages ON packages_tags.package_id = packages_packages.id')
            .select('packages_tags.id, packages_packages.project_id')

          ApplicationRecord.connection.execute <<~SQL
            WITH joined_cte(packages_tag_id, project_id) AS MATERIALIZED (
              #{joined.to_sql}
            )
            UPDATE packages_tags
            SET project_id = joined_cte.project_id
            FROM joined_cte
            WHERE id = joined_cte.packages_tag_id
          SQL
        end
      end
    end
  end
end