Commit da7233d4 authored by Małgorzata Ksionek's avatar Małgorzata Ksionek

Add cr remarks

parent 056cedde
# frozen_string_literal: true # frozen_string_literal: true
class EnsureNamespaceSettingsCreation < ActiveRecord::Migration[5.2] class EnsureNamespaceSettingsCreation < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false DOWNTIME = false
BATCH_SIZE = 10000 BATCH_SIZE = 10000
MIGRATION = 'BackfillNamespaceSettings'
DELAY_INTERVAL = 2.minutes.to_i
disable_ddl_transaction!
class Namespace < ActiveRecord::Base class Namespace < ActiveRecord::Base
include EachBatch include EachBatch
...@@ -21,12 +27,13 @@ class EnsureNamespaceSettingsCreation < ActiveRecord::Migration[5.2] ...@@ -21,12 +27,13 @@ class EnsureNamespaceSettingsCreation < ActiveRecord::Migration[5.2]
private private
def ensure_data_migration def ensure_data_migration
Namespace.each_batch(of: BATCH_SIZE) do |query| Namespace.each_batch(of: BATCH_SIZE) do |query, index|
missing_count = query.where("NOT EXISTS (SELECT 1 FROM namespace_settings WHERE namespace_settings.namespace_id=namespaces.id)").limit(1).size missing_count = query.where("NOT EXISTS (SELECT 1 FROM namespace_settings WHERE namespace_settings.namespace_id=namespaces.id)").limit(1).size
if missing_count > 0 if missing_count > 0
min, max = query.pluck("MIN(id), MAX(id)").flatten ids_range = query.pluck("MIN(id), MAX(id)").flatten
# we expect low record count so inline execution is fine.
Gitlab::BackgroundMigration::BackfillNamespaceSettings.new.perform(min, max) migrate_in(index * DELAY_INTERVAL, MIGRATION, ids_range)
end end
end end
end end
......
...@@ -8,9 +8,37 @@ RSpec.describe EnsureNamespaceSettingsCreation do ...@@ -8,9 +8,37 @@ RSpec.describe EnsureNamespaceSettingsCreation do
let(:namespaces) { table(:namespaces) } let(:namespaces) { table(:namespaces) }
let(:namespace_settings) { table(:namespace_settings) } let(:namespace_settings) { table(:namespace_settings) }
let!(:namespace) { namespaces.create!(name: 'gitlab', path: 'gitlab-org') } let!(:namespace) { namespaces.create!(name: 'gitlab', path: 'gitlab-org') }
let!(:namespace_2) { namespaces.create!(name: 'gitlab', path: 'gitlab-org2') }
it 'migrates namespaces without namespace_settings' do it 'migrates namespaces without namespace_settings' do
expect { migrate! }.to change { namespace_settings.count }.from(0).to(1) stub_const("#{described_class.name}::BATCH_SIZE", 2)
Sidekiq::Testing.fake! do
freeze_time do
migrate!
expect(described_class::MIGRATION)
.to be_scheduled_delayed_migration(2.minutes.to_i, namespace.id, namespace_2.id)
end
end
end
it 'schedules migrations in batches ' do
stub_const("#{described_class.name}::BATCH_SIZE", 2)
namespace_3 = namespaces.create!(name: 'gitlab', path: 'gitlab-org3')
namespace_4 = namespaces.create!(name: 'gitlab', path: 'gitlab-org4')
Sidekiq::Testing.fake! do
freeze_time do
migrate!
expect(described_class::MIGRATION)
.to be_scheduled_delayed_migration(2.minutes.to_i, namespace.id, namespace_2.id)
expect(described_class::MIGRATION)
.to be_scheduled_delayed_migration(4.minutes.to_i, namespace_3.id, namespace_4.id)
end
end
end end
end 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