Class: Gitlab::BackgroundMigration::FixFirstMentionedInCommitAt

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/background_migration/fix_first_mentioned_in_commit_at.rb

Overview

Class that fixes the incorrectly set authored_date within issue_metrics table

Defined Under Namespace

Classes: TmpIssueMetrics

Constant Summary collapse

SUB_BATCH_SIZE =
500

Instance Method Summary collapse

Instance Method Details

#perform(start_id, end_id) ⇒ Object

rubocop: enable Style/Documentation



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gitlab/background_migration/fix_first_mentioned_in_commit_at.rb', line 30

def perform(start_id, end_id)
  scope(start_id, end_id).each_batch(of: SUB_BATCH_SIZE, column: :issue_id) do |sub_batch|
    first, last = sub_batch.pick(Arel.sql('min(issue_id), max(issue_id)'))

    # The query need to be reconstructed because .each_batch modifies the default scope
    # See: https://gitlab.com/gitlab-org/gitlab/-/issues/330510
    inner_query = TmpIssueMetrics
      .unscoped
      .merge(scope(first, last))
      .from("issue_metrics, #{lateral_query}")
      .select('issue_metrics.issue_id', 'first_authored_date.authored_date')
      .where('issue_metrics.first_mentioned_in_commit_at > first_authored_date.authored_date')

    TmpIssueMetrics.connection.execute <<~UPDATE_METRICS
      WITH cte AS MATERIALIZED (
        #{inner_query.to_sql}
      )
      UPDATE issue_metrics
      SET
        first_mentioned_in_commit_at = cte.authored_date
      FROM
        cte
      WHERE
        cte.issue_id = issue_metrics.issue_id
    UPDATE_METRICS
  end

  mark_job_as_succeeded(start_id, end_id)
end