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

20231005131445_add_work_items_related_link_restrictions.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e26f6a367618a7ef9f97a82138531543c425619c (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true

class AddWorkItemsRelatedLinkRestrictions < Gitlab::Database::Migration[2.1]
  RELATED = 0
  BLOCKS = 1

  class WorkItemType < MigrationRecord
    self.table_name = 'work_item_types'
  end

  class RelatedLinkRestriction < MigrationRecord
    self.table_name = 'work_item_related_link_restrictions'
  end

  restrict_gitlab_migration gitlab_schema: :gitlab_main
  disable_ddl_transaction!

  # rubocop:disable Metrics/AbcSize
  def up
    epic = WorkItemType.find_by_name_and_namespace_id('Epic', nil)
    issue = WorkItemType.find_by_name_and_namespace_id('Issue', nil)
    task = WorkItemType.find_by_name_and_namespace_id('Task', nil)
    objective = WorkItemType.find_by_name_and_namespace_id('Objective', nil)
    key_result = WorkItemType.find_by_name_and_namespace_id('Key Result', nil)

    unless epic && issue && task && objective && key_result
      Gitlab::AppLogger.warn('Default WorkItemType records are missing, not adding RelatedLinkRestrictions.')

      return
    end

    restrictions = [
      { source_type_id: epic.id, target_type_id: epic.id, link_type: RELATED },
      { source_type_id: epic.id, target_type_id: issue.id, link_type: RELATED },
      { source_type_id: epic.id, target_type_id: task.id, link_type: RELATED },
      { source_type_id: epic.id, target_type_id: objective.id, link_type: RELATED },
      { source_type_id: epic.id, target_type_id: key_result.id, link_type: RELATED },
      { source_type_id: issue.id, target_type_id: issue.id, link_type: RELATED },
      { source_type_id: issue.id, target_type_id: task.id, link_type: RELATED },
      { source_type_id: issue.id, target_type_id: objective.id, link_type: RELATED },
      { source_type_id: issue.id, target_type_id: key_result.id, link_type: RELATED },
      { source_type_id: task.id, target_type_id: task.id, link_type: RELATED },
      { source_type_id: task.id, target_type_id: objective.id, link_type: RELATED },
      { source_type_id: task.id, target_type_id: key_result.id, link_type: RELATED },
      { source_type_id: objective.id, target_type_id: objective.id, link_type: RELATED },
      { source_type_id: objective.id, target_type_id: key_result.id, link_type: RELATED },
      { source_type_id: key_result.id, target_type_id: key_result.id, link_type: RELATED },
      { source_type_id: epic.id, target_type_id: epic.id, link_type: BLOCKS },
      { source_type_id: epic.id, target_type_id: issue.id, link_type: BLOCKS },
      { source_type_id: epic.id, target_type_id: task.id, link_type: BLOCKS },
      { source_type_id: epic.id, target_type_id: objective.id, link_type: BLOCKS },
      { source_type_id: epic.id, target_type_id: key_result.id, link_type: BLOCKS },
      { source_type_id: issue.id, target_type_id: issue.id, link_type: BLOCKS },
      { source_type_id: issue.id, target_type_id: epic.id, link_type: BLOCKS },
      { source_type_id: issue.id, target_type_id: task.id, link_type: BLOCKS },
      { source_type_id: issue.id, target_type_id: objective.id, link_type: BLOCKS },
      { source_type_id: issue.id, target_type_id: key_result.id, link_type: BLOCKS },
      { source_type_id: task.id, target_type_id: task.id, link_type: BLOCKS },
      { source_type_id: task.id, target_type_id: epic.id, link_type: BLOCKS },
      { source_type_id: task.id, target_type_id: issue.id, link_type: BLOCKS },
      { source_type_id: task.id, target_type_id: objective.id, link_type: BLOCKS },
      { source_type_id: task.id, target_type_id: key_result.id, link_type: BLOCKS },
      { source_type_id: objective.id, target_type_id: objective.id, link_type: BLOCKS },
      { source_type_id: objective.id, target_type_id: key_result.id, link_type: BLOCKS },
      { source_type_id: key_result.id, target_type_id: key_result.id, link_type: BLOCKS },
      { source_type_id: key_result.id, target_type_id: objective.id, link_type: BLOCKS }
    ]

    RelatedLinkRestriction.upsert_all(
      restrictions,
      unique_by: :index_work_item_link_restrictions_on_source_link_type_target
    )
  end
  # rubocop:enable Metrics/AbcSize

  def down
    # Until this point the restrictions table was empty so we can delete all records when migrating down
    RelatedLinkRestriction.delete_all
  end
end