Commit 08bee854 authored by Markus Koller's avatar Markus Koller

Don't delete unrelated index when indexing wiki

When using Gitlab::Elastic::Indexer with a wiki repository, the call to
delete_index_for_commits_and_blobs caused the existing commits and blobs
for the project repository to be deleted from the index.
parent 2f409a9a
...@@ -318,7 +318,14 @@ module Elasticsearch ...@@ -318,7 +318,14 @@ module Elasticsearch
res res
end end
def delete_index_for_commits_and_blobs def delete_index_for_commits_and_blobs(wiki: false)
types =
if wiki
%w[wiki_blob]
else
%w[commit blob]
end
client_for_indexing.delete_by_query( client_for_indexing.delete_by_query(
index: self.class.index_name, index: self.class.index_name,
routing: es_parent, routing: es_parent,
...@@ -328,7 +335,7 @@ module Elasticsearch ...@@ -328,7 +335,7 @@ module Elasticsearch
filter: [ filter: [
{ {
terms: { terms: {
type: %w{commit blob} type: types
} }
}, },
{ {
......
...@@ -92,7 +92,7 @@ module Gitlab ...@@ -92,7 +92,7 @@ module Gitlab
def run_indexer!(to_sha) def run_indexer!(to_sha)
if index_status && !repository_contains_last_indexed_commit? if index_status && !repository_contains_last_indexed_commit?
repository.delete_index_for_commits_and_blobs repository.delete_index_for_commits_and_blobs(wiki: wiki?)
end end
command = command =
......
...@@ -31,7 +31,7 @@ describe Gitlab::Elastic::Indexer do ...@@ -31,7 +31,7 @@ describe Gitlab::Elastic::Indexer do
end end
context 'wikis' do context 'wikis' do
let!(:project) { create(:project, :wiki_repo) } let(:project) { create(:project, :wiki_repo) }
let(:indexer) { described_class.new(project, wiki: true) } let(:indexer) { described_class.new(project, wiki: true) }
before do before do
...@@ -66,6 +66,57 @@ describe Gitlab::Elastic::Indexer do ...@@ -66,6 +66,57 @@ describe Gitlab::Elastic::Indexer do
indexer.run indexer.run
end end
context 'when IndexStatus#last_wiki_commit is no longer in repository', :elastic do
let(:user) { project.owner }
let(:ee_application_setting) { nil }
before do
stub_ee_application_setting(elasticsearch_indexing: true)
ElasticIndexerWorker.new.perform('index', 'Project', project.id, project.es_id)
end
def change_wiki_and_index(project, &blk)
yield blk if blk
current_commit = project.wiki.repository.commit('master').sha
described_class.new(project, wiki: true).run(current_commit)
Gitlab::Elastic::Helper.refresh_index
end
def indexed_wiki_paths_for(term)
blobs = ProjectWiki.search(
term,
type: :wiki_blob
)[:wiki_blobs][:results].response
blobs.map do |blob|
blob['_source']['blob']['path']
end
end
it 'reindexes from scratch' do
sha_for_reset = nil
change_wiki_and_index(project) do
sha_for_reset = project.wiki.repository.create_file(user, '12', '', message: '12', branch_name: 'master')
project.wiki.repository.create_file(user, '23', '', message: '23', branch_name: 'master')
end
expect(indexed_wiki_paths_for('12')).to include('12')
expect(indexed_wiki_paths_for('23')).to include('23')
project.index_status.update!(last_wiki_commit: '____________')
change_wiki_and_index(project) do
project.wiki.repository.write_ref('master', sha_for_reset)
end
expect(indexed_wiki_paths_for('12')).to include('12')
expect(indexed_wiki_paths_for('23')).not_to include('23')
end
end
end end
context 'repository has unborn head' do context 'repository has unborn head' do
...@@ -250,10 +301,10 @@ describe Gitlab::Elastic::Indexer do ...@@ -250,10 +301,10 @@ describe Gitlab::Elastic::Indexer do
context 'when IndexStatus#last_commit is no longer in repository' do context 'when IndexStatus#last_commit is no longer in repository' do
before do before do
ElasticIndexerWorker.new.perform("index", "Project", project.id, project.es_id) ElasticIndexerWorker.new.perform('index', 'Project', project.id, project.es_id)
end end
it 'reindexes from scratch if IndexStatus#last_commit is no longer in repository' do it 'reindexes from scratch' do
sha_for_reset = nil sha_for_reset = nil
change_repository_and_index(project) do change_repository_and_index(project) do
...@@ -264,7 +315,7 @@ describe Gitlab::Elastic::Indexer do ...@@ -264,7 +315,7 @@ describe Gitlab::Elastic::Indexer do
expect(indexed_file_paths_for('12')).to include('12') expect(indexed_file_paths_for('12')).to include('12')
expect(indexed_file_paths_for('23')).to include('23') expect(indexed_file_paths_for('23')).to include('23')
project.index_status.update(last_commit: '____________') project.index_status.update!(last_commit: '____________')
change_repository_and_index(project) do change_repository_and_index(project) do
project.repository.write_ref('master', sha_for_reset) project.repository.write_ref('master', sha_for_reset)
......
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