Commit 2f219efa authored by Yannis Roussos's avatar Yannis Roussos

Merge branch 'ab/reindex-single' into 'master'

Fix single-index invocation

See merge request gitlab-org/gitlab!50061
parents 429f4a23 478a6dc6
...@@ -192,12 +192,18 @@ namespace :gitlab do ...@@ -192,12 +192,18 @@ namespace :gitlab do
exit exit
end end
indexes = if args[:index_name] indexes = Gitlab::Database::Reindexing.candidate_indexes
[Gitlab::Database::PostgresIndex.by_identifier(args[:index_name])]
else if identifier = args[:index_name]
Gitlab::Database::Reindexing.candidate_indexes raise ArgumentError, "Index name is not fully qualified with a schema: #{identifier}" unless identifier =~ /^\w+\.\w+$/
indexes = indexes.where(identifier: identifier)
raise "Index not found or not supported: #{args[:index_name]}" if indexes.empty?
end end
ActiveRecord::Base.logger = Logger.new(STDOUT) if Gitlab::Utils.to_boolean(ENV['LOG_QUERIES_TO_CONSOLE'], default: false)
Gitlab::Database::Reindexing.perform(indexes) Gitlab::Database::Reindexing.perform(indexes)
rescue => e rescue => e
Gitlab::AppLogger.error(e) Gitlab::AppLogger.error(e)
......
...@@ -246,17 +246,25 @@ RSpec.describe 'gitlab:db namespace rake task' do ...@@ -246,17 +246,25 @@ RSpec.describe 'gitlab:db namespace rake task' do
context 'with index name given' do context 'with index name given' do
let(:index) { double('index') } let(:index) { double('index') }
before do
allow(Gitlab::Database::Reindexing).to receive(:candidate_indexes).and_return(indexes)
end
it 'calls the index rebuilder with the proper arguments' do it 'calls the index rebuilder with the proper arguments' do
expect(Gitlab::Database::PostgresIndex).to receive(:by_identifier).with('public.foo_idx').and_return(index) allow(indexes).to receive(:where).with(identifier: 'public.foo_idx').and_return([index])
expect(Gitlab::Database::Reindexing).to receive(:perform).with([index]) expect(Gitlab::Database::Reindexing).to receive(:perform).with([index])
run_rake_task('gitlab:db:reindex', '[public.foo_idx]') run_rake_task('gitlab:db:reindex', '[public.foo_idx]')
end end
it 'raises an error if the index does not exist' do it 'raises an error if the index does not exist' do
expect(Gitlab::Database::PostgresIndex).to receive(:by_identifier).with('public.absent_index').and_raise(ActiveRecord::RecordNotFound) allow(indexes).to receive(:where).with(identifier: 'public.absent_index').and_return([])
expect { run_rake_task('gitlab:db:reindex', '[public.absent_index]') }.to raise_error(/Index not found/)
end
expect { run_rake_task('gitlab:db:reindex', '[public.absent_index]') }.to raise_error(ActiveRecord::RecordNotFound) it 'raises an error if the index is not fully qualified with a schema' do
expect { run_rake_task('gitlab:db:reindex', '[foo_idx]') }.to raise_error(/Index name is not fully qualified/)
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