Skip to content

Commit

Permalink
Revert to creating index asynchronously
Browse files Browse the repository at this point in the history
Creating the index synchronously was required to get an up to version of
its settings, in order to compare them to the user configuration and
decide whether or not to update settings. Unfortunately this made
practically every operation have a synchronous component causing a lot
of problems down the line.
This commit reverts to using create_index asynchronously to avoid said
issues, and represents the settings of a nonexistent index as an empty
hash, which will queue an update_settings if the user has specified any
settings at all.
  • Loading branch information
ellnix committed Feb 19, 2024
1 parent 29f59c8 commit c7654a9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/meilisearch-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def initialize(index_uid, raise_on_failure, options)
@raise_on_failure = raise_on_failure.nil? || raise_on_failure

SafeIndex.log_or_throw(nil, @raise_on_failure) do
client.create_index!(index_uid, { primary_key: primary_key })
client.create_index(index_uid, { primary_key: primary_key })
end

@index = client.index(index_uid)
Expand Down Expand Up @@ -306,7 +306,7 @@ def settings(*args)
SafeIndex.log_or_throw(:settings, @raise_on_failure) do
@index.settings(*args)
rescue ::MeiliSearch::ApiError => e
return {} if e.code == 404 # not fatal
return {} if e.code == 'index_not_found' # not fatal

raise e
end
Expand Down
11 changes: 5 additions & 6 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1136,23 +1136,22 @@
let(:index_instance) { instance_double(MeiliSearch::Index, settings: nil, update_settings: nil) }
let(:slow_client) { instance_double(MeiliSearch::Client, index: index_instance) }

before do
allow(slow_client).to receive(:create_index)
allow(MeiliSearch::Rails).to receive(:client).and_return(slow_client)
end

it 'does not raise error timeouts on reindex' do
allow(index_instance).to receive(:add_documents).and_raise(MeiliSearch::TimeoutError)
allow(slow_client).to receive(:create_index!).and_return(index_instance)

allow(MeiliSearch::Rails).to receive(:client).and_return(slow_client)

expect do
Vegetable.create(name: 'potato')
end.not_to raise_error
end

it 'does not raise error timeouts on data addition' do
allow(slow_client).to receive(:create_index!).and_raise(MeiliSearch::TimeoutError)
allow(index_instance).to receive(:add_documents).and_return(nil)

allow(MeiliSearch::Rails).to receive(:client).and_return(slow_client)

expect do
Vegetable.ms_reindex!
end.not_to raise_error
Expand Down

0 comments on commit c7654a9

Please sign in to comment.