Commit 019e70a8 authored by Brett Walker's avatar Brett Walker Committed by Luke Duncalfe

Backfill `member_namespace_id` for `GroupMember`

For `GroupMembers`, the `member_namespace_id` ==
`source_id`.  Copy this over.  `ProjectMember` will be handled
in another migration.

Changelog: changed
parent 91ddcecb
# frozen_string_literal: true
class TempIndexForGroupNamespaceMemberBackfill < Gitlab::Database::Migration[1.0]
INDEX_NAME = 'tmp_index_for_namespace_id_migration_on_group_members'
disable_ddl_transaction!
def up
# Temporary index to be removed in 14.10
# https://gitlab.com/gitlab-org/gitlab/-/issues/353538
add_concurrent_index :members, :id, where: "members.member_namespace_id IS NULL and members.type = 'GroupMember'", name: INDEX_NAME
end
def down
remove_concurrent_index_by_name :members, INDEX_NAME
end
end
# frozen_string_literal: true
class BackfillMemberNamespaceIdForGroupMembers < Gitlab::Database::Migration[1.0]
MIGRATION = 'BackfillMemberNamespaceForGroupMembers'
INTERVAL = 2.minutes
BATCH_SIZE = 1_000
MAX_BATCH_SIZE = 2_000
SUB_BATCH_SIZE = 100
def up
queue_batched_background_migration(
MIGRATION,
:members,
:id,
job_interval: INTERVAL,
batch_size: BATCH_SIZE,
max_batch_size: MAX_BATCH_SIZE,
sub_batch_size: SUB_BATCH_SIZE
)
end
def down
Gitlab::Database::BackgroundMigration::BatchedMigration
.for_configuration(MIGRATION, :members, :id, [])
.delete_all
end
end
68b45f97a2165c934f097ca976fb27ffcb533c57facee95344e3985b5cfd8347
\ No newline at end of file
dd3a4209a72b470a14a3acc5d06db1f5fec67cb4f19b20b2e4d7d94b302fe122
\ No newline at end of file
......@@ -28479,6 +28479,8 @@ CREATE INDEX tmp_idx_vulnerability_occurrences_on_id_where_report_type_7_99 ON v
CREATE INDEX tmp_index_container_repositories_on_id_migration_state ON container_repositories USING btree (id, migration_state);
CREATE INDEX tmp_index_for_namespace_id_migration_on_group_members ON members USING btree (id) WHERE ((member_namespace_id IS NULL) AND ((type)::text = 'GroupMember'::text));
CREATE INDEX tmp_index_for_namespace_id_migration_on_routes ON routes USING btree (id) WHERE ((namespace_id IS NULL) AND ((source_type)::text = 'Namespace'::text));
CREATE INDEX tmp_index_members_on_state ON members USING btree (state) WHERE (state = 2);
# frozen_string_literal: true
module Gitlab
module BackgroundMigration
# Backfills the `members.member_namespace_id` column for `type=GroupMember`
class BackfillMemberNamespaceForGroupMembers
include Gitlab::Database::DynamicModelHelpers
def perform(start_id, end_id, batch_table, batch_column, sub_batch_size, pause_ms)
parent_batch_relation = relation_scoped_to_range(batch_table, batch_column, start_id, end_id)
parent_batch_relation.each_batch(column: batch_column, of: sub_batch_size) do |sub_batch|
batch_metrics.time_operation(:update_all) do
sub_batch.update_all('member_namespace_id=source_id')
end
pause_ms = [0, pause_ms].max
sleep(pause_ms * 0.001)
end
end
def batch_metrics
@batch_metrics ||= Gitlab::Database::BackgroundMigration::BatchMetrics.new
end
private
def relation_scoped_to_range(source_table, source_key_column, start_id, stop_id)
define_batchable_model(source_table, connection: ActiveRecord::Base.connection)
.joins('INNER JOIN namespaces ON members.source_id = namespaces.id')
.where(source_key_column => start_id..stop_id)
.where(type: 'GroupMember')
.where(source_type: 'Namespace')
.where(member_namespace_id: nil)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::BackfillMemberNamespaceForGroupMembers, :migration, schema: 20220120211832 do
let(:migration) { described_class.new }
let(:members_table) { table(:members) }
let(:namespaces_table) { table(:namespaces) }
let(:table_name) { 'members' }
let(:batch_column) { :id }
let(:sub_batch_size) { 100 }
let(:pause_ms) { 0 }
subject(:perform_migration) { migration.perform(1, 10, table_name, batch_column, sub_batch_size, pause_ms) }
before do
namespaces_table.create!(id: 100, name: 'test1', path: 'test1', type: 'Group')
namespaces_table.create!(id: 101, name: 'test2', path: 'test2', type: 'Group')
namespaces_table.create!(id: 102, name: 'test3', path: 'test3', type: 'Group')
namespaces_table.create!(id: 201, name: 'test4', path: 'test4', type: 'Project')
members_table.create!(id: 1, source_id: 100, source_type: 'Namespace', type: 'GroupMember', member_namespace_id: nil, access_level: 10, notification_level: 3)
members_table.create!(id: 2, source_id: 101, source_type: 'Namespace', type: 'GroupMember', member_namespace_id: nil, access_level: 10, notification_level: 3)
members_table.create!(id: 3, source_id: 102, source_type: 'Namespace', type: 'GroupMember', member_namespace_id: 102, access_level: 10, notification_level: 3)
members_table.create!(id: 4, source_id: 103, source_type: 'Project', type: 'ProjectMember', member_namespace_id: nil, access_level: 10, notification_level: 3)
members_table.create!(id: 5, source_id: 104, source_type: 'Project', type: 'ProjectMember', member_namespace_id: 201, access_level: 10, notification_level: 3)
end
it 'backfills `member_namespace_id` for the selected records', :aggregate_failures do
expect(members_table.where(type: 'GroupMember', member_namespace_id: nil).count).to eq 2
expect(members_table.where(type: 'ProjectMember', member_namespace_id: nil).count).to eq 1
queries = ActiveRecord::QueryRecorder.new do
perform_migration
end
expect(queries.count).to eq(3)
expect(members_table.where(type: 'GroupMember', member_namespace_id: nil).count).to eq 0
expect(members_table.where(type: 'GroupMember').pluck(:member_namespace_id)).to match_array([100, 101, 102])
expect(members_table.where(type: 'ProjectMember', member_namespace_id: nil).count).to eq 1
expect(members_table.where(type: 'ProjectMember').pluck(:member_namespace_id)).to match_array([nil, 201])
end
it 'tracks timings of queries' do
expect(migration.batch_metrics.timings).to be_empty
expect { perform_migration }.to change { migration.batch_metrics.timings }
end
end
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe BackfillMemberNamespaceIdForGroupMembers do
let_it_be(:migration) { described_class::MIGRATION }
describe '#up' do
it 'schedules background jobs for each batch of group members' do
migrate!
expect(migration).to have_scheduled_batched_migration(
table_name: :members,
column_name: :id,
interval: described_class::INTERVAL
)
end
end
describe '#down' do
it 'deletes all batched migration records' do
migrate!
schema_migrate_down!
expect(migration).not_to have_scheduled_batched_migration
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