Commit 77da5a2c authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'georgekoltsov/fix-post-deployment-migration-for-group-import-states' into 'master'

Fix CleanupGroupImportStatesWithNullUserId post deploy migration

See merge request gitlab-org/gitlab!42987
parents 8b508303 3e0b81c7
......@@ -19,7 +19,6 @@ class CleanupGroupImportStatesWithNullUserId < ActiveRecord::Migration[6.0]
class Namespace < ActiveRecord::Base
self.table_name = 'namespaces'
belongs_to :parent, class_name: 'CleanupGroupImportStatesWithNullUserId::Namespace'
belongs_to :owner, class_name: 'CleanupGroupImportStatesWithNullUserId::User'
end
......@@ -39,6 +38,10 @@ class CleanupGroupImportStatesWithNullUserId < ActiveRecord::Migration[6.0]
owners.first || parent&.default_owner || owner
end
def parent
Group.find_by_id(parent_id)
end
def owners
Member.where(type: 'GroupMember', source_type: 'Namespace', source_id: id, requested_at: nil, access_level: OWNER).map(&:user)
end
......
......@@ -61,6 +61,32 @@ RSpec.describe CleanupGroupImportStatesWithNullUserId, :migration,
expect { group_import_state_3.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
context 'when group has parent' do
it 'updates user_id with parent group default owner id' do
user = users_table.create!(name: 'user4', email: 'user4@example.com', projects_limit: 1)
group_1 = namespaces_table.create!(name: 'group_1', path: 'group_1', type: 'Group')
create_member(user_id: user.id, type: 'GroupMember', source_type: 'Namespace', source_id: group_1.id, access_level: described_class::Group::OWNER)
group_2 = namespaces_table.create!(name: 'group_2', path: 'group_2', type: 'Group', parent_id: group_1.id)
group_import_state = group_import_states_table.create!(group_id: group_2.id, user_id: nil, status: 0)
disable_migrations_output { migrate! }
expect(group_import_state.reload.user_id).to eq(user.id)
end
end
context 'when group has owner_id' do
it 'updates user_id with owner_id' do
user = users_table.create!(name: 'user', email: 'user@example.com', projects_limit: 1)
group = namespaces_table.create!(name: 'group', path: 'group', type: 'Group', owner_id: user.id)
group_import_state = group_import_states_table.create!(group_id: group.id, user_id: nil, status: 0)
disable_migrations_output { migrate! }
expect(group_import_state.reload.user_id).to eq(user.id)
end
end
end
def create_member(options)
......
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