lib/searchkick/index.rb in searchkick-4.1.1 vs lib/searchkick/index.rb in searchkick-4.2.0
- old
+ new
@@ -45,11 +45,11 @@
def settings
client.indices.get_settings index: name
end
def refresh_interval
- settings.values.first["settings"]["index"]["refresh_interval"]
+ index_settings["refresh_interval"]
end
def update_settings(settings)
client.indices.put_settings index: name, body: settings
end
@@ -247,20 +247,29 @@
locations = Array(options[:locations])
locations.map(&:to_s) + locations.map(&:to_sym)
end
end
+ # private
+ def uuid
+ index_settings["uuid"]
+ end
+
protected
def client
Searchkick.client
end
def bulk_indexer
@bulk_indexer ||= BulkIndexer.new(self)
end
+ def index_settings
+ settings.values.first["settings"]["index"]
+ end
+
def import_before_promotion(index, relation, **import_options)
index.import_scope(relation, **import_options)
end
# https://gist.github.com/jarosan/3124884
@@ -283,17 +292,20 @@
async: async,
full: true,
scope: scope
}
+ uuid = index.uuid
+
# check if alias exists
alias_exists = alias_exists?
if alias_exists
import_before_promotion(index, relation, **import_options) if import
# get existing indices to remove
unless async
+ check_uuid(uuid, index.uuid)
promote(index.name, update_refresh_interval: !refresh_interval.nil?)
clean_indices unless retain
end
else
delete if exists?
@@ -314,10 +326,11 @@
puts "Batches left: #{status[:batches_left]}"
end
# already promoted if alias didn't exist
if alias_exists
puts "Jobs complete. Promoting..."
+ check_uuid(uuid, index.uuid)
promote(index.name, update_refresh_interval: !refresh_interval.nil?)
end
clean_indices unless retain
puts "SUCCESS!"
end
@@ -331,8 +344,18 @@
if e.message.include?("No handler for type [text]")
raise UnsupportedVersionError, "This version of Searchkick requires Elasticsearch 5 or greater"
end
raise e
+ end
+
+ # safety check
+ # still a chance for race condition since its called before promotion
+ # ideal is for user to disable automatic index creation
+ # https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-creation
+ def check_uuid(old_uuid, new_uuid)
+ if old_uuid != new_uuid
+ raise Searchkick::Error, "Safety check failed - only run one Model.reindex per model at a time"
+ end
end
end
end