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

20210608110760_add_requirement_test_reports_foreign_key.rb « migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e256bce6ae094a58e4ce359c0f6dc47916595414 (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
# frozen_string_literal: true

class AddRequirementTestReportsForeignKey < ActiveRecord::Migration[6.1]
  include Gitlab::Database::MigrationHelpers

  disable_ddl_transaction!

  TARGET_TABLE = :requirements_management_test_reports
  CONSTRAINT_NAME = 'requirements_test_reports_requirement_id_xor_issue_id'

  def up
    add_concurrent_foreign_key TARGET_TABLE, :issues, column: :issue_id

    add_check_constraint(TARGET_TABLE, 'num_nonnulls(requirement_id, issue_id) = 1', CONSTRAINT_NAME)
  end

  def down
    remove_check_constraint TARGET_TABLE, CONSTRAINT_NAME

    with_lock_retries do
      remove_foreign_key_if_exists(TARGET_TABLE, column: :issue_id)
    end
  end
end