Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .ruby-version

This file was deleted.

19 changes: 18 additions & 1 deletion lib/waistband/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def initialize(index_name, options = {})
@subs = @subs.flatten if @subs.is_a?(Array)
end

if options['force_name'].present?
@force_name = options['force_name']

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to call this force_name versus alias_name?

Copy link
Author

@bsampietro bsampietro Jan 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because an index name could also be used. It is an overwriting from the name on the config file.

end

end

def exists?
Expand Down Expand Up @@ -261,6 +265,18 @@ def alias_exists?(alias_name)
)
end

def parent_index_names
client.indices.get_alias(name: config_name).map { |k, v| k }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
client.indices.get_alias(name: config_name).map { |k, v| k }
client.indices.get_alias(name: config_name).keys

rescue Elasticsearch::Transport::Transport::Errors::NotFound
nil

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather return an empty array here and keep the return type consistent.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, much better

end

def get_first_parent_index_reference
index_name = parent_index_names&.first
return nil if index_name.nil?
self.class.new(@index_name, force_name: index_name)
end

def config
::Waistband.config.index @index_name
end
Expand Down Expand Up @@ -345,7 +361,7 @@ def full_alias_name(alias_name)
end

def custom_name?
!!config['name']
!!config['name'] || !!(defined? @force_name)
end

def stringify_all(data)
Expand Down Expand Up @@ -381,6 +397,7 @@ def settings
end

def config_name
return @force_name if defined? @force_name
@subs ? "#{base_config_name}__#{@subs.join('_')}" : base_config_name
end

Expand Down
72 changes: 72 additions & 0 deletions spec/lib/index/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,26 @@

end

describe 'force_name' do

let(:forced_name_index) { Waistband::Index.new 'events_no_name', force_name: 'new_forced_name' }

around(:each) do |example|
forced_name_index.delete
example.run
forced_name_index.delete
end

it "creates the index" do
expect(forced_name_index.exists?).to be_falsey

forced_name_index.create!
expect(forced_name_index.exists?).to be_truthy
expect(forced_name_index.send(:config_name)).to eql 'new_forced_name'
end

end

describe '#base_config_name' do

it "gets a default name that makes sense for the index when not defined" do
Expand Down Expand Up @@ -348,6 +368,58 @@

end

it 'returns the name of the parent index' do
index.alias('events_alias')
alias_ref = Waistband::Index.new 'events', force_name: index.send(:full_alias_name, 'events_alias')

parent_index_names = alias_ref.parent_index_names
expect(parent_index_names.length).to eq(1)
expect(parent_index_names[0]).to eq(index.send(:config_name))
end

it 'can return a reference to the parent index' do
index.alias('events_alias')
alias_ref = Waistband::Index.new 'events', force_name: index.send(:full_alias_name, 'events_alias')
parent_index = alias_ref.get_first_parent_index_reference

expect(parent_index.exists?).to be_truthy
expect(parent_index.send(:config_name)).to eq(index.send(:config_name))
end

it "can save read and update documents from an alias" do
index.alias('events_alias')
alias_ref = Waistband::Index.new 'events', force_name: index.send(:full_alias_name, 'events_alias')

expect(alias_ref.save('__test_write', {'ok' => 'yeah', 'not_ok' => 'yeah'})).to be_present
expect(alias_ref.update('__test_write', {'not_ok' => 'no'})).to be_present
expect(alias_ref.read('__test_write')).to eql({
'_id' => '__test_write',
'_index' => 'events_test',
'_source' => {'ok' => 'yeah', 'not_ok' => 'no'},
'_type' => 'event',
'_version' => 2,
'found' => true
})
end

it "updates mappings from an alias" do
index = Waistband::Index.new 'geo', force_name: 'geo_index'
index.delete
index.create!
index.alias('geo_index_alias')

alias_ref = Waistband::Index.new 'geo', force_name: 'geo_index_alias'

responses = alias_ref.update_all_mappings
index.delete

expect(responses).to be_an Array

response = responses.first
expect(response['acknowledged']).to be_truthy
expect(response['_type']).to eq('geo')
end

end

describe 'logging' do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'rspec'
require 'timecop'
require 'active_support/core_ext/integer/time'
require 'json'

unless ENV['ON_TRAVIS']
require 'byebug'
Expand Down