20201124075951_create_vulnerability_external_links.rb 1.64 KB
Newer Older
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
# frozen_string_literal: true

class CreateVulnerabilityExternalLinks < ActiveRecord::Migration[6.0]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    with_lock_retries do
      create_table :vulnerability_external_issue_links, if_not_exists: true do |t|
        t.timestamps_with_timezone null: false
        t.references :author, null: false, index: true, foreign_key: { to_table: :users, on_delete: :nullify }, type: :bigint
        t.references :vulnerability, null: false, index: true, type: :bigint
        t.integer :link_type, limit: 2, null: false, default: 1 # 'created'
        t.integer :external_type, limit: 2, null: false, default: 1 # 'jira'
        t.text :external_project_key, null: false
        t.text :external_issue_key, null: false

        t.index %i[vulnerability_id external_type external_project_key external_issue_key],
          name: 'idx_vulnerability_ext_issue_links_on_vulne_id_and_ext_issue',
          unique: true
        t.index %i[vulnerability_id link_type],
          name: 'idx_vulnerability_ext_issue_links_on_vulne_id_and_link_type',
          where: 'link_type = 1',
          unique: true # only one 'created' link per vulnerability is allowed
      end
    end

    add_concurrent_foreign_key :vulnerability_external_issue_links, :vulnerabilities, column: :vulnerability_id, on_delete: :cascade

    add_text_limit :vulnerability_external_issue_links, :external_project_key, 255
    add_text_limit :vulnerability_external_issue_links, :external_issue_key, 255
  end

  def down
    with_lock_retries do
      drop_table :vulnerability_external_issue_links
    end
  end
end