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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'db/post_migrate/20210823132600_remove_duplicate_dast_site_tokens.rb')
-rw-r--r--db/post_migrate/20210823132600_remove_duplicate_dast_site_tokens.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/db/post_migrate/20210823132600_remove_duplicate_dast_site_tokens.rb b/db/post_migrate/20210823132600_remove_duplicate_dast_site_tokens.rb
new file mode 100644
index 00000000000..35cf3b55200
--- /dev/null
+++ b/db/post_migrate/20210823132600_remove_duplicate_dast_site_tokens.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+class RemoveDuplicateDastSiteTokens < ActiveRecord::Migration[6.1]
+ disable_ddl_transaction!
+
+ class DastSiteToken < ApplicationRecord
+ self.table_name = 'dast_site_tokens'
+ self.inheritance_column = :_type_disabled
+
+ scope :duplicates, -> do
+ all_duplicates = select(:project_id, :url)
+ .distinct
+ .group(:project_id, :url)
+ .having('count(*) > 1')
+ .pluck('array_agg(id) as ids')
+
+ duplicate_ids = extract_duplicate_ids(all_duplicates)
+
+ where(id: duplicate_ids)
+ end
+
+ def self.extract_duplicate_ids(duplicates)
+ duplicates.flat_map { |ids| ids.first(ids.size - 1) }
+ end
+ end
+
+ def up
+ DastSiteToken.duplicates.delete_all
+ end
+
+ def down
+ end
+end