summaryrefslogtreecommitdiff
path: root/db/migrate/20201215132151_change_unique_index_on_security_findings.rb
diff options
context:
space:
mode:
Diffstat (limited to 'db/migrate/20201215132151_change_unique_index_on_security_findings.rb')
-rw-r--r--db/migrate/20201215132151_change_unique_index_on_security_findings.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/db/migrate/20201215132151_change_unique_index_on_security_findings.rb b/db/migrate/20201215132151_change_unique_index_on_security_findings.rb
new file mode 100644
index 00000000000..fe474ef3991
--- /dev/null
+++ b/db/migrate/20201215132151_change_unique_index_on_security_findings.rb
@@ -0,0 +1,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