[go: up one dir, main page]

Prepare temporary index to support event.target_types backfill for project events

What does this MR do and why?

Prepare temporary index to be used for Create `BackfillEventsTargetTypeForProjects` ba... (#565788 - closed)

PoC for the complete solution: Draft: Resolve "'target_type' parameter for Eve... (!202890 - closed)

Backfill script

# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    class BackfillProjectEventsTargetType < BatchedMigrationJob
      PROJECT_ACTIONS = [1, 5, 8, 9, 11].freeze # Defined in Events::PROJECT_ACTIONS

      # rubocop:disable Database/AvoidScopeTo -- supported by an index `tmp_idx_events_missing_project_target_type`
      scope_to ->(relation) { relation.where(target_type: nil, action: PROJECT_ACTIONS).where.not(project_id: nil) }
      # rubocop:enable Database/AvoidScopeTo

      operation_name :back_project_events_target_type
      feature_category :groups_and_projects

      def perform
        each_sub_batch do |sub_batch|
          sub_batch.update_all(
            target_type: 'Project',
            target_id: Arel.sql('project_id')
          )
        end
      end
    end
  end
end

Example batching query plan: https://console.postgres.ai/gitlab/gitlab-production-main/sessions/43148/commands/131931

References

Screenshots or screen recordings

N/A

How to set up and validate locally

Run bin/rails db:migrate

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Shane Maglangit

Merge request reports

Loading