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
path: root/lib
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-11-24 06:12:33 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2021-11-24 06:12:33 +0300
commit492f99eac8d349e03e22d9c3eed8923b80bcc297 (patch)
tree0d18b29a4086f9f5b4958687b3ee33ef2df8b5c3 /lib
parent1c27dcaf69cd0181755172d94c87c823e4d5f069 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/background_migration/remove_occurrence_pipelines_and_duplicate_vulnerabilities_findings.rb59
-rw-r--r--lib/gitlab/database/schema_cache_with_renamed_table.rb2
2 files changed, 60 insertions, 1 deletions
diff --git a/lib/gitlab/background_migration/remove_occurrence_pipelines_and_duplicate_vulnerabilities_findings.rb b/lib/gitlab/background_migration/remove_occurrence_pipelines_and_duplicate_vulnerabilities_findings.rb
new file mode 100644
index 00000000000..7fe5a427d10
--- /dev/null
+++ b/lib/gitlab/background_migration/remove_occurrence_pipelines_and_duplicate_vulnerabilities_findings.rb
@@ -0,0 +1,59 @@
+# frozen_string_literal: true
+
+# This migration will look for Vulnerabilities::Finding objects that would have a duplicate UUIDv5 if the UUID was
+# recalculated. Then it removes Vulnerabilities::FindingPipeline objects associated with those Findings.
+# We can't just drop those Findings directly since the cascade drop will timeout if any given Finding has too many
+# associated FindingPipelines
+class Gitlab::BackgroundMigration::RemoveOccurrencePipelinesAndDuplicateVulnerabilitiesFindings
+ # rubocop:disable Gitlab/NamespacedClass, Style/Documentation
+ class VulnerabilitiesFinding < ActiveRecord::Base
+ self.table_name = "vulnerability_occurrences"
+ end
+
+ class VulnerabilitiesFindingPipeline < ActiveRecord::Base
+ include EachBatch
+ self.table_name = "vulnerability_occurrence_pipelines"
+ end
+ # rubocop:enable Gitlab/NamespacedClass, Style/Documentation
+
+ def perform(start_id, end_id)
+ ids_to_look_for = findings_that_would_produce_duplicate_uuids(start_id, end_id)
+
+ ids_to_look_for.each do |finding_id|
+ VulnerabilitiesFindingPipeline.where(occurrence_id: finding_id).each_batch(of: 1000) do |pipelines|
+ pipelines.delete_all
+ end
+ end
+
+ VulnerabilitiesFinding.where(id: ids_to_look_for).delete_all
+
+ mark_job_as_succeeded(start_id, end_id)
+ end
+
+ private
+
+ def findings_that_would_produce_duplicate_uuids(start_id, end_id)
+ VulnerabilitiesFinding
+ .from("vulnerability_occurrences to_delete")
+ .where("to_delete.id BETWEEN ? AND ?", start_id, end_id)
+ .where(
+ "EXISTS (
+ SELECT 1
+ FROM vulnerability_occurrences duplicates
+ WHERE duplicates.report_type = to_delete.report_type
+ AND duplicates.location_fingerprint = to_delete.location_fingerprint
+ AND duplicates.primary_identifier_id = to_delete.primary_identifier_id
+ AND duplicates.project_id = to_delete.project_id
+ AND duplicates.id > to_delete.id
+ )"
+ )
+ .pluck("to_delete.id")
+ end
+
+ def mark_job_as_succeeded(*arguments)
+ Gitlab::Database::BackgroundMigrationJob.mark_all_as_succeeded(
+ self.class.name.demodulize,
+ arguments
+ )
+ end
+end
diff --git a/lib/gitlab/database/schema_cache_with_renamed_table.rb b/lib/gitlab/database/schema_cache_with_renamed_table.rb
index 28123edd708..74900dc0d26 100644
--- a/lib/gitlab/database/schema_cache_with_renamed_table.rb
+++ b/lib/gitlab/database/schema_cache_with_renamed_table.rb
@@ -42,7 +42,7 @@ module Gitlab
def renamed_tables_cache
@renamed_tables ||= begin
Gitlab::Database::TABLES_TO_BE_RENAMED.select do |old_name, new_name|
- ActiveRecord::Base.connection.view_exists?(old_name)
+ connection.view_exists?(old_name)
end
end
end