Skip to content
Snippets Groups Projects
Commit 0166a696 authored by Madelein van Niekerk's avatar Madelein van Niekerk :one: Committed by Dmitry Gruzd
Browse files

Adds an Elastic migration helper for creating a new index

Changelog: changed
EE: true
parent 59503bf3
No related branches found
No related tags found
1 merge request!122183Elastic migration helper for creating a new index
......@@ -54,6 +54,7 @@ Search/NamespacedClass:
- 'ee/app/workers/concerns/elastic/bulk_cron_worker.rb'
- 'ee/app/workers/concerns/elastic/indexing_control.rb'
- 'ee/app/workers/concerns/elastic/migration_backfill_helper.rb'
- 'ee/app/workers/concerns/elastic/migration_create_index.rb'
- 'ee/app/workers/concerns/elastic/migration_helper.rb'
- 'ee/app/workers/concerns/elastic/migration_obsolete.rb'
- 'ee/app/workers/concerns/elastic/migration_options.rb'
......
......@@ -152,6 +152,34 @@ class MigrationName < Elastic::Migration
end
```
### `Elastic::MigrationCreateIndex`
Creates a new index.
Requires:
- The `target_class` and `document_type` methods
- Mappings and index settings for the class in `ee/lib/elastic/latest/` and `ee/lib/elastic/v12p1/`
WARNING:
You must perform a follow-up migration to populate the index in the same milestone.
```ruby
class MigrationName < Elastic::Migration
include Elastic::MigrationCreateIndex
retry_on_failure
def document_type
:epic
end
def target_class
Epic
end
end
```
#### `Elastic::MigrationHelper`
Contains methods you can use when a migration doesn't fit the previous examples.
......
# frozen_string_literal: true
module Elastic
module MigrationCreateIndex
include Elastic::MigrationHelper
def migrate
reindexing_cleanup!
log "Creating standalone #{document_type} index #{new_index_name}"
helper.create_standalone_indices(target_classes: [target_class])
rescue StandardError => e
log('Failed to create index', error: e.message)
raise StandardError, e.message
end
def completed?
helper.index_exists?(index_name: new_index_name)
end
def target_class
raise NotImplementedError
end
def document_type
raise NotImplementedError
end
end
end
......@@ -200,6 +200,66 @@ def update_by_query(objects, script)
end
end
RSpec.shared_examples 'migration creates a new index' do |version, klass|
let(:helper) { Gitlab::Elastic::Helper.new }
before do
allow(subject).to receive(:helper).and_return(helper)
end
subject { described_class.new(version) }
describe '#migrate' do
it 'logs a message and creates a standalone index' do
expect(subject).to receive(:log).with(/Creating standalone .* index/)
expect(helper).to receive(:create_standalone_indices).with(target_classes: [klass]).and_return(true).once
subject.migrate
end
describe 'reindexing_cleanup!' do
context 'when the index already exists' do
before do
allow(helper).to receive(:index_exists?).and_return(true)
allow(helper).to receive(:create_standalone_indices).and_return(true)
end
it 'deletes the index' do
expect(helper).to receive(:delete_index).once
subject.migrate
end
end
end
context 'when an error is raised' do
let(:error) { 'oops' }
before do
allow(helper).to receive(:create_standalone_indices).and_raise(StandardError, error)
allow(subject).to receive(:log).and_return(true)
end
it 'logs a message and raises an error' do
expect(subject).to receive(:log).with(/Failed to create index/, error: error)
expect { subject.migrate }.to raise_error(StandardError, error)
end
end
end
describe '#completed?' do
[true, false].each do |matcher|
it 'returns true if the index exists' do
allow(helper).to receive(:create_standalone_indices).and_return(true)
allow(helper).to receive(:index_exists?).with(index_name: /gitlab-test-/).and_return(matcher)
expect(subject.completed?).to eq(matcher)
end
end
end
end
RSpec.shared_examples 'a deprecated Advanced Search migration' do |version|
subject { described_class.new(version) }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment