summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/reset_duplicate_ci_runners_token_encrypted_values.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-10-20 09:40:42 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2022-10-20 09:40:42 +0000
commitee664acb356f8123f4f6b00b73c1e1cf0866c7fb (patch)
treef8479f94a28f66654c6a4f6fb99bad6b4e86a40e /lib/gitlab/background_migration/reset_duplicate_ci_runners_token_encrypted_values.rb
parent62f7d5c5b69180e82ae8196b7b429eeffc8e7b4f (diff)
downloadgitlab-ce-ee664acb356f8123f4f6b00b73c1e1cf0866c7fb.tar.gz
Add latest changes from gitlab-org/gitlab@15-5-stable-eev15.5.0-rc42
Diffstat (limited to 'lib/gitlab/background_migration/reset_duplicate_ci_runners_token_encrypted_values.rb')
-rw-r--r--lib/gitlab/background_migration/reset_duplicate_ci_runners_token_encrypted_values.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/gitlab/background_migration/reset_duplicate_ci_runners_token_encrypted_values.rb b/lib/gitlab/background_migration/reset_duplicate_ci_runners_token_encrypted_values.rb
new file mode 100644
index 00000000000..952f3b0e3c3
--- /dev/null
+++ b/lib/gitlab/background_migration/reset_duplicate_ci_runners_token_encrypted_values.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module BackgroundMigration
+ # A job to nullify duplicate token_encrypted values in ci_runners table in batches
+ class ResetDuplicateCiRunnersTokenEncryptedValues < BatchedMigrationJob
+ def perform
+ each_sub_batch(operation_name: :nullify_duplicate_ci_runner_token_encrypted_values) do |sub_batch|
+ # Reset duplicate runner encrypted tokens that would prevent creating an unique index.
+ nullify_duplicate_ci_runner_token_encrypted_values(sub_batch)
+ end
+ end
+
+ private
+
+ def nullify_duplicate_ci_runner_token_encrypted_values(sub_batch)
+ batchable_model = define_batchable_model(batch_table, connection: connection)
+
+ duplicate_tokens = batchable_model
+ .where(token_encrypted: sub_batch.select(:token_encrypted).distinct)
+ .group(:token_encrypted)
+ .having('COUNT(*) > 1')
+ .pluck(:token_encrypted)
+
+ return if duplicate_tokens.empty?
+
+ batchable_model.where(token_encrypted: duplicate_tokens).update_all(token_encrypted: nil)
+ end
+ end
+ end
+end