Commit 8c4879e7 authored by Pavel Shutsin's avatar Pavel Shutsin

Cleanup used membership invites

Changelog: other
parent 9ee6c875
# frozen_string_literal: true
class CleanupRemainingOrphanInvites < ActiveRecord::Migration[6.1]
include Gitlab::Database::MigrationHelpers
disable_ddl_transaction!
TMP_INDEX_NAME = 'tmp_idx_members_with_orphaned_invites'
QUERY_CONDITION = "invite_token IS NOT NULL AND user_id IS NOT NULL"
def up
membership = define_batchable_model('members')
add_concurrent_index :members, :id, where: QUERY_CONDITION, name: TMP_INDEX_NAME
membership.where(QUERY_CONDITION).pluck(:id).each_slice(10) do |group|
membership.where(id: group).where(QUERY_CONDITION).update_all(invite_token: nil)
end
remove_concurrent_index_by_name :members, TMP_INDEX_NAME
end
def down
remove_concurrent_index_by_name :members, TMP_INDEX_NAME if index_exists_by_name?(:members, TMP_INDEX_NAME)
end
end
5dc6a4f9ecbd705bf8361c65b29931cde94968084e8ae7945a27acdcbd6475c8
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require_migration! 'cleanup_remaining_orphan_invites'
RSpec.describe CleanupRemainingOrphanInvites, :migration do
def create_member(**extra_attributes)
defaults = {
access_level: 10,
source_id: 1,
source_type: "Project",
notification_level: 0,
type: 'ProjectMember'
}
table(:members).create!(defaults.merge(extra_attributes))
end
def create_user(**extra_attributes)
defaults = { projects_limit: 0 }
table(:users).create!(defaults.merge(extra_attributes))
end
describe '#up', :aggregate_failures do
it 'removes invite tokens for accepted records' do
record1 = create_member(invite_token: 'foo', user_id: nil)
record2 = create_member(invite_token: 'foo2', user_id: create_user(username: 'foo', email: 'foo@example.com').id)
record3 = create_member(invite_token: nil, user_id: create_user(username: 'bar', email: 'bar@example.com').id)
migrate!
expect(table(:members).find(record1.id).invite_token).to eq 'foo'
expect(table(:members).find(record2.id).invite_token).to eq nil
expect(table(:members).find(record3.id).invite_token).to eq nil
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment