Commit 99038e56 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '341069-backfill-namespace-db-entries-with-correct-usernamespace-type' into 'master'

Backfill namespace DB entries with correct UserNamespace type

See merge request gitlab-org/gitlab!71510
parents c9ab16a2 1d8e8f3e
# frozen_string_literal: true
class BackfillUserNamespace < Gitlab::Database::Migration[1.0]
MIGRATION = 'BackfillUserNamespace'
INTERVAL = 2.minutes
BATCH_SIZE = 1_000
SUB_BATCH_SIZE = 200
DOWNTIME = false
def up
queue_batched_background_migration(
MIGRATION,
:namespaces,
:id,
job_interval: INTERVAL,
batch_size: BATCH_SIZE,
sub_batch_size: SUB_BATCH_SIZE
)
end
def down
Gitlab::Database::BackgroundMigration::BatchedMigration
.for_configuration(MIGRATION, :namespaces, :id, [])
.delete_all
end
end
3aaf2a4fa834331768e2acc10f67b8d456e70aca9784787e40b55dade7b6f64c
\ No newline at end of file
# frozen_string_literal: true
module Gitlab
module BackgroundMigration
# Backfills the `namespaces.type` column, replacing any
# instances of `NULL` with `User`
class BackfillUserNamespace
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, order_hint: :type) do |sub_batch|
batch_metrics.time_operation(:update_all) do
sub_batch.update_all(type: 'User')
end
pause_ms = 0 if pause_ms < 0
sleep(pause_ms * 0.001)
end
end
def batch_metrics
@batch_metrics ||= Gitlab::Database::BackgroundMigration::BatchMetrics.new
end
private
def connection
ActiveRecord::Base.connection
end
def relation_scoped_to_range(source_table, source_key_column, start_id, stop_id)
define_batchable_model(source_table)
.where(source_key_column => start_id..stop_id)
.where(type: nil)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::BackfillUserNamespace, :migration, schema: 20210930211936 do
let(:migration) { described_class.new }
let(:namespaces_table) { table(:namespaces) }
let(:table_name) { 'namespaces' }
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: 1, name: 'test1', path: 'test1', type: nil)
namespaces_table.create!(id: 2, name: 'test2', path: 'test2', type: 'User')
namespaces_table.create!(id: 3, name: 'test3', path: 'test3', type: 'Group')
namespaces_table.create!(id: 4, name: 'test4', path: 'test4', type: nil)
namespaces_table.create!(id: 11, name: 'test11', path: 'test11', type: nil)
end
it 'backfills `type` for the selected records', :aggregate_failures do
queries = ActiveRecord::QueryRecorder.new do
perform_migration
end
expect(queries.count).to eq(3)
expect(namespaces_table.where(type: 'User').count).to eq 3
expect(namespaces_table.where(type: 'User').pluck(:id)).to match_array([1, 2, 4])
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 BackfillUserNamespace do
let_it_be(:migration) { described_class::MIGRATION }
describe '#up' do
it 'schedules background jobs for each batch of namespaces' do
migrate!
expect(migration).to have_scheduled_batched_migration(
table_name: :namespaces,
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