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:
authorGitLab Bot <gitlab-bot@gitlab.com>2023-07-19 17:16:28 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2023-07-19 17:16:28 +0300
commite4384360a16dd9a19d4d2d25d0ef1f2b862ed2a6 (patch)
tree2fcdfa7dcdb9db8f5208b2562f4b4e803d671243 /db/post_migrate/20230707211358_deduplicate_inactive_alert_integrations.rb
parentffda4e7bcac36987f936b4ba515995a6698698f0 (diff)
Add latest changes from gitlab-org/gitlab@16-2-stable-eev16.2.0-rc42
Diffstat (limited to 'db/post_migrate/20230707211358_deduplicate_inactive_alert_integrations.rb')
-rw-r--r--db/post_migrate/20230707211358_deduplicate_inactive_alert_integrations.rb40
1 files changed, 40 insertions, 0 deletions
diff --git a/db/post_migrate/20230707211358_deduplicate_inactive_alert_integrations.rb b/db/post_migrate/20230707211358_deduplicate_inactive_alert_integrations.rb
new file mode 100644
index 00000000000..041ce659da6
--- /dev/null
+++ b/db/post_migrate/20230707211358_deduplicate_inactive_alert_integrations.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+class DeduplicateInactiveAlertIntegrations < Gitlab::Database::Migration[2.1]
+ restrict_gitlab_migration gitlab_schema: :gitlab_main
+
+ class HttpIntegration < MigrationRecord
+ self.table_name = 'alert_management_http_integrations'
+ end
+
+ # Existing unique index ensures that all active integrations are already unique.
+ # Any non-unique records must be inactive by definition, but dups are unlikely.
+ # At time of writing, gitlab.com has 0 records in this state.
+ # Of the ~1600 integrations, only ~100 are even inactive.
+ def up
+ duplicated_identifiers.each do |(project_id, endpoint_identifier)|
+ find_inactive_integrations(project_id, endpoint_identifier).delete_all
+ end
+ end
+
+ def down
+ # no-op -> unable to identify duplicates retroactively
+ end
+
+ private
+
+ def duplicated_identifiers
+ HttpIntegration
+ .group(:project_id, :endpoint_identifier)
+ .having('count(id) > 1')
+ .pluck(:project_id, :endpoint_identifier)
+ end
+
+ def find_inactive_integrations(project_id, endpoint_identifier)
+ HttpIntegration.where(
+ project_id: project_id,
+ endpoint_identifier: endpoint_identifier,
+ active: false
+ )
+ end
+end