Skip to content

Commit b0bfab1

Browse files
Merge #330
330: Fix settings update regression r=ellnix a=ellnix # Pull Request ## Related issue Fixes #329 May fix #328 ## What does this PR do? - `SafeIndex` before I tried to fix #280 with #301 did not guarantee that its index exists at all, since ``@index`` was created asynchronously it was possible that `SafeIndex#settings` could be called on an index that does not exist. This line: https://github.com/meilisearch/meilisearch-rails/blob/29f59c88881b5a4b5a03d990f11d3aac220cd367/lib/meilisearch-rails.rb#L309 was supposed return an empty hash when asked to fetch the settings of an index that does not exist, however `ApiError#code` is the meilisearch code (`"index_not_found"`) and not the http code (`404`). That line was therefore skipped and the `index_not_found` error was being propagated and caught by the `rescue nil` in: https://github.com/meilisearch/meilisearch-rails/blob/e5ad4d1f10c078097bf211e4d3a6e1d48b24bbc5/lib/meilisearch-rails.rb#L750 until I removed it and tried to replace it with ensuring that `SafeIndex` had an index by making `create_index!` synchronous and all kinds of hell broke loose since now every time an index was used would cause a synchronous wait. Co-authored-by: ellnix <[email protected]>
2 parents 29f59c8 + 03a8daa commit b0bfab1

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

lib/meilisearch-rails.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def initialize(index_uid, raise_on_failure, options)
261261
@raise_on_failure = raise_on_failure.nil? || raise_on_failure
262262

263263
SafeIndex.log_or_throw(nil, @raise_on_failure) do
264-
client.create_index!(index_uid, { primary_key: primary_key })
264+
client.create_index(index_uid, { primary_key: primary_key })
265265
end
266266

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

311311
raise e
312312
end
@@ -843,7 +843,7 @@ def meilisearch_settings_changed?(server_state, user_configuration)
843843
if user.is_a?(Hash) && server.is_a?(Hash)
844844
meilisearch_settings_changed?(server, user)
845845
elsif user.is_a?(Array) && server.is_a?(Array)
846-
user.map(&:to_s) != server.map(&:to_s)
846+
user.map(&:to_s).sort! != server.map(&:to_s).sort!
847847
else
848848
user.to_s != server.to_s
849849
end

spec/integration_spec.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,23 +1136,22 @@
11361136
let(:index_instance) { instance_double(MeiliSearch::Index, settings: nil, update_settings: nil) }
11371137
let(:slow_client) { instance_double(MeiliSearch::Client, index: index_instance) }
11381138

1139+
before do
1140+
allow(slow_client).to receive(:create_index)
1141+
allow(MeiliSearch::Rails).to receive(:client).and_return(slow_client)
1142+
end
1143+
11391144
it 'does not raise error timeouts on reindex' do
11401145
allow(index_instance).to receive(:add_documents).and_raise(MeiliSearch::TimeoutError)
1141-
allow(slow_client).to receive(:create_index!).and_return(index_instance)
1142-
1143-
allow(MeiliSearch::Rails).to receive(:client).and_return(slow_client)
11441146

11451147
expect do
11461148
Vegetable.create(name: 'potato')
11471149
end.not_to raise_error
11481150
end
11491151

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

1154-
allow(MeiliSearch::Rails).to receive(:client).and_return(slow_client)
1155-
11561155
expect do
11571156
Vegetable.ms_reindex!
11581157
end.not_to raise_error

0 commit comments

Comments
 (0)