Commit e20ac49685322b75606ed2055a1856aa90aef413

Authored by Andrew Kane
1 parent 85923349

Added safety check for multiple Model.reindex - #1316 #1351

Showing 2 changed files with 20 additions and 0 deletions   Show diff stats
CHANGELOG.md
1 1 ## 4.1.2 (unreleased)
2 2  
  3 +- Added safety check for multiple `Model.reindex`
3 4 - Added `deep_paging` option
4 5  
5 6 ## 4.1.1 (2019-11-19)
... ...
lib/searchkick/index.rb
... ... @@ -249,6 +249,11 @@ module Searchkick
249 249 end
250 250 end
251 251  
  252 + # private
  253 + def uuid
  254 + settings.values.first["settings"]["index"]["uuid"]
  255 + end
  256 +
252 257 protected
253 258  
254 259 def client
... ... @@ -285,6 +290,8 @@ module Searchkick
285 290 scope: scope
286 291 }
287 292  
  293 + uuid = index.uuid
  294 +
288 295 # check if alias exists
289 296 alias_exists = alias_exists?
290 297 if alias_exists
... ... @@ -292,6 +299,7 @@ module Searchkick
292 299  
293 300 # get existing indices to remove
294 301 unless async
  302 + check_uuid(uuid, index.uuid)
295 303 promote(index.name, update_refresh_interval: !refresh_interval.nil?)
296 304 clean_indices unless retain
297 305 end
... ... @@ -316,6 +324,7 @@ module Searchkick
316 324 # already promoted if alias didn't exist
317 325 if alias_exists
318 326 puts "Jobs complete. Promoting..."
  327 + check_uuid(uuid, index.uuid)
319 328 promote(index.name, update_refresh_interval: !refresh_interval.nil?)
320 329 end
321 330 clean_indices unless retain
... ... @@ -334,5 +343,15 @@ module Searchkick
334 343  
335 344 raise e
336 345 end
  346 +
  347 + # safety check
  348 + # still a chance for race condition since its called before promotion
  349 + # ideal is for user to disable automatic index creation
  350 + # https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-creation
  351 + def check_uuid(old_uuid, new_uuid)
  352 + if old_uuid != new_uuid
  353 + raise Searchkick::Error, "Safety check failed - only run one Model.reindex per model at a time"
  354 + end
  355 + end
337 356 end
338 357 end
... ...