Class: Gitlab::BackgroundMigration::BackfillResourceLinkEvents

Inherits:
BatchedMigrationJob show all
Defined in:
lib/gitlab/background_migration/backfill_resource_link_events.rb

Overview

Backfills resource_link_events from system_note_metadata and notes records

Defined Under Namespace

Classes: ResourceLinkEvent

Constant Summary

Constants inherited from BatchedMigrationJob

Gitlab::BackgroundMigration::BatchedMigrationJob::DEFAULT_FEATURE_CATEGORY

Constants included from Database::DynamicModelHelpers

Database::DynamicModelHelpers::BATCH_SIZE

Instance Method Summary collapse

Methods inherited from BatchedMigrationJob

#batch_metrics, feature_category, #filter_batch, generic_instance, #initialize, job_arguments, job_arguments_count, operation_name, scope_to

Methods included from Database::DynamicModelHelpers

#define_batchable_model, #each_batch, #each_batch_range

Constructor Details

This class inherits a constructor from Gitlab::BackgroundMigration::BatchedMigrationJob

Instance Method Details

#performObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gitlab/background_migration/backfill_resource_link_events.rb', line 22

def perform
  each_sub_batch do |sub_batch|
    values_subquery = resource_link_event_values_query(sub_batch.select(:id).to_sql)

    connection.execute(<<~SQL)
      INSERT INTO resource_link_events (action, issue_id, child_work_item_id, user_id, created_at, system_note_metadata_id)
       #{values_subquery}
      ON CONFLICT (system_note_metadata_id) DO NOTHING;
    SQL
  end
end


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
59
60
61
62
63
64
65
66
67
68
# File 'lib/gitlab/background_migration/backfill_resource_link_events.rb', line 34

def resource_link_event_values_query(ids_subquery)
  <<~SQL
    SELECT
      CASE WHEN system_note_metadata.action='relate_to_parent' THEN #{ResourceLinkEvent.actions[:add]}
      ELSE #{ResourceLinkEvent.actions[:remove]}
      END AS action,
      parent_issues.id AS issue_id,
      notes.noteable_id AS child_work_item_id,
      notes.author_id AS user_id,
      system_note_metadata.created_at AS created_at,
      system_note_metadata.id AS system_note_metadata_id
    FROM system_note_metadata
      INNER JOIN notes ON system_note_metadata.note_id = notes.id
      INNER JOIN issues as work_items ON work_items.id = notes.noteable_id,
    LATERAL (
      -- This lateral join searches for the id of the parent issue.
      --
      -- When a child work item is added to its parent,
      --   "relate_to_parent" is recorded as `system_note_metadata.action`
      --    and a note records to which parent the child work item is added e.g, "added #1 (iid) as parent".
      --
      -- Based on the iid of the parent extracted from the note and using the child work item's project id,
      -- we can find out the id of the parent issue.
      SELECT issues.id
      FROM issues
      WHERE
        issues.project_id = work_items.project_id
        AND issues.iid = CASE WHEN system_note_metadata.action='relate_to_parent' THEN substring(notes.note from 'added #(\\d+) as parent')::bigint
                          ELSE  substring(notes.note from 'removed parent \\S+ #(\\d+)')::bigint
                          END
      ) parent_issues
    WHERE
      system_note_metadata.id IN (#{ids_subquery})
  SQL
end