summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/destroy_invalid_project_members.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/background_migration/destroy_invalid_project_members.rb')
-rw-r--r--lib/gitlab/background_migration/destroy_invalid_project_members.rb25
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/gitlab/background_migration/destroy_invalid_project_members.rb b/lib/gitlab/background_migration/destroy_invalid_project_members.rb
new file mode 100644
index 00000000000..3c60f765c29
--- /dev/null
+++ b/lib/gitlab/background_migration/destroy_invalid_project_members.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module BackgroundMigration
+ class DestroyInvalidProjectMembers < Gitlab::BackgroundMigration::BatchedMigrationJob # rubocop:disable Style/Documentation
+ scope_to ->(relation) { relation.where(source_type: 'Project') }
+
+ def perform
+ each_sub_batch(operation_name: :delete_all) do |sub_batch|
+ invalid_project_members = sub_batch
+ .joins('LEFT OUTER JOIN projects ON members.source_id = projects.id')
+ .where(projects: { id: nil })
+ invalid_ids = invalid_project_members.pluck(:id)
+
+ # the actual delete
+ deleted_count = invalid_project_members.delete_all
+
+ Gitlab::AppLogger.info({ message: 'Removing invalid project member records',
+ deleted_count: deleted_count,
+ ids: invalid_ids })
+ end
+ end
+ end
+ end
+end