summaryrefslogtreecommitdiff
path: root/db/migrate/20201215132151_change_unique_index_on_security_findings.rb
blob: fe474ef3991ca3ee89525ee90bcd3502fba5f476 (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
# frozen_string_literal: true

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

  DOWNTIME = false
  OLD_INDEX_NAME = 'index_security_findings_on_uuid'
  NEW_INDEX_NAME = 'index_security_findings_on_uuid_and_scan_id'

  disable_ddl_transaction!

  class SecurityFinding < ActiveRecord::Base
    include EachBatch

    self.table_name = 'security_findings'
  end

  def up
    add_concurrent_index :security_findings, [:uuid, :scan_id], unique: true, name: NEW_INDEX_NAME

    remove_concurrent_index_by_name :security_findings, OLD_INDEX_NAME
  end

  def down
    # It is very unlikely that we rollback this migration but just in case if we have to,
    # we have to clear the table because there can be multiple records with the same UUID
    # which would break the creation of unique index on the `uuid` column.
    # We choose clearing the table because just removing the duplicated records would
    # cause data inconsistencies.
    SecurityFinding.each_batch(of: 10000) { |relation| relation.delete_all }

    add_concurrent_index :security_findings, :uuid, unique: true, name: OLD_INDEX_NAME

    remove_concurrent_index_by_name :security_findings, NEW_INDEX_NAME
  end
end