Commit c44d5921 authored by Gosia Ksionek's avatar Gosia Ksionek Committed by Adam Hegyi

Ensure namespace settings are backfilled

parent 96fcb5ba
---
title: Ensure namespace settings are backfilled via migration
merge_request: 41679
author:
type: other
# frozen_string_literal: true
class CompleteNamespaceSettingsMigration < ActiveRecord::Migration[5.2]
DOWNTIME = false
BATCH_SIZE = 10000
class Namespace < ActiveRecord::Base
include EachBatch
self.table_name = 'namespaces'
end
def up
Gitlab::BackgroundMigration.steal('BackfillNamespaceSettings')
ensure_data_migration
end
def down
# no-op
end
private
def ensure_data_migration
Namespace.each_batch(of: BATCH_SIZE) do |query|
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
min, max = query.pluck("MIN(id), MAX(id)").flatten
# we expect low record count so inline execution is fine.
Gitlab::BackgroundMigration::BackfillNamespaceSettings.new.perform(min, max)
end
end
end
end
2311967a9f68e1a428662e0231752ad0d844063d66cca895211d38f9ae928d94
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200907124300_complete_namespace_settings_migration.rb')
RSpec.describe CompleteNamespaceSettingsMigration, :redis do
let(:migration) { spy('migration') }
context 'when still legacy artifacts exist' do
let(:namespaces) { table(:namespaces) }
let(:namespace_settings) { table(:namespace_settings) }
let!(:namespace) { namespaces.create!(name: 'gitlab', path: 'gitlab-org') }
it 'steals sidekiq jobs from BackfillNamespaceSettings background migration' do
expect(Gitlab::BackgroundMigration).to receive(:steal).with('BackfillNamespaceSettings')
migrate!
end
it 'migrates namespaces without namespace_settings' do
expect { migrate! }.to change { namespace_settings.count }.from(0).to(1)
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