Commit 1f5c350661f9707324be59d9189d03745feb1e3f
1 parent
45be88e1
Exists in
master
and in
19 other branches
Added index_suffix option - #891
Showing
8 changed files
with
39 additions
and
8 deletions
Show diff stats
CHANGELOG.md
README.md
... | ... | @@ -1770,6 +1770,14 @@ product = FactoryGirl.create(:product) |
1770 | 1770 | product.reindex(refresh: true) |
1771 | 1771 | ``` |
1772 | 1772 | |
1773 | +### Parallel Tests | |
1774 | + | |
1775 | +Set: | |
1776 | + | |
1777 | +```ruby | |
1778 | +Searchkick.index_suffix = ENV["TEST_ENV_NUMBER"] | |
1779 | +``` | |
1780 | + | |
1773 | 1781 | ## Multi-Tenancy |
1774 | 1782 | |
1775 | 1783 | Check out [this great post](https://www.tiagoamaro.com.br/2014/12/11/multi-tenancy-with-searchkick/) on the [Apartment](https://github.com/influitive/apartment) gem. Follow a similar pattern if you use another gem. | ... | ... |
Rakefile
lib/searchkick.rb
... | ... | @@ -36,7 +36,7 @@ module Searchkick |
36 | 36 | class ImportError < Error; end |
37 | 37 | |
38 | 38 | class << self |
39 | - attr_accessor :search_method_name, :wordnet_path, :timeout, :models, :client_options, :redis | |
39 | + attr_accessor :search_method_name, :wordnet_path, :timeout, :models, :client_options, :redis, :index_suffix | |
40 | 40 | attr_writer :client, :env, :search_timeout |
41 | 41 | attr_reader :aws_credentials |
42 | 42 | end | ... | ... |
lib/searchkick/model.rb
... | ... | @@ -21,8 +21,8 @@ module Searchkick |
21 | 21 | class_variable_set :@@searchkick_klass, self |
22 | 22 | class_variable_set :@@searchkick_callbacks, callbacks |
23 | 23 | class_variable_set :@@searchkick_index, options[:index_name] || |
24 | - (options[:index_prefix].respond_to?(:call) && proc { [options[:index_prefix].call, model_name.plural, Searchkick.env].compact.join("_") }) || | |
25 | - [options[:index_prefix], model_name.plural, Searchkick.env].compact.join("_") | |
24 | + (options[:index_prefix].respond_to?(:call) && proc { [options[:index_prefix].call, model_name.plural, Searchkick.env, Searchkick.index_suffix].compact.join("_") }) || | |
25 | + [options[:index_prefix], model_name.plural, Searchkick.env, Searchkick.index_suffix].compact.join("_") | |
26 | 26 | |
27 | 27 | class << self |
28 | 28 | def searchkick_search(term = "*", **options, &block) | ... | ... |
test/index_test.rb
... | ... | @@ -7,8 +7,9 @@ class IndexTest < Minitest::Test |
7 | 7 | end |
8 | 8 | |
9 | 9 | def test_clean_indices |
10 | - old_index = Searchkick::Index.new("products_test_20130801000000000") | |
11 | - different_index = Searchkick::Index.new("items_test_20130801000000000") | |
10 | + suffix = Searchkick.index_suffix ? "_#{Searchkick.index_suffix}" : "" | |
11 | + old_index = Searchkick::Index.new("products_test#{suffix}_20130801000000000") | |
12 | + different_index = Searchkick::Index.new("items_test#{suffix}_20130801000000000") | |
12 | 13 | |
13 | 14 | old_index.delete if old_index.exists? |
14 | 15 | different_index.delete if different_index.exists? |
... | ... | @@ -25,7 +26,8 @@ class IndexTest < Minitest::Test |
25 | 26 | end |
26 | 27 | |
27 | 28 | def test_clean_indices_old_format |
28 | - old_index = Searchkick::Index.new("products_test_20130801000000") | |
29 | + suffix = Searchkick.index_suffix ? "_#{Searchkick.index_suffix}" : "" | |
30 | + old_index = Searchkick::Index.new("products_test#{suffix}_20130801000000") | |
29 | 31 | old_index.create |
30 | 32 | |
31 | 33 | Product.searchkick_index.clean_indices | ... | ... |
test/test_helper.rb
... | ... | @@ -6,11 +6,16 @@ require "logger" |
6 | 6 | require "active_support/core_ext" if defined?(NoBrainer) |
7 | 7 | require "active_support/notifications" |
8 | 8 | |
9 | +Searchkick.index_suffix = ENV["TEST_ENV_NUMBER"] | |
10 | + | |
9 | 11 | ENV["RACK_ENV"] = "test" |
10 | 12 | |
11 | 13 | Minitest::Test = Minitest::Unit::TestCase unless defined?(Minitest::Test) |
12 | 14 | |
13 | -File.delete("elasticsearch.log") if File.exist?("elasticsearch.log") | |
15 | +if !defined?(ParallelTests) || ParallelTests.first_process? | |
16 | + File.delete("elasticsearch.log") if File.exist?("elasticsearch.log") | |
17 | +end | |
18 | + | |
14 | 19 | Searchkick.client.transport.logger = Logger.new("elasticsearch.log") |
15 | 20 | Searchkick.search_timeout = 5 |
16 | 21 | |
... | ... | @@ -466,7 +471,7 @@ class Animal |
466 | 471 | searchkick \ |
467 | 472 | text_start: [:name], |
468 | 473 | suggest: [:name], |
469 | - index_name: -> { "#{name.tableize}-#{Date.today.year}" }, | |
474 | + index_name: -> { "#{name.tableize}-#{Date.today.year}#{Searchkick.index_suffix}" }, | |
470 | 475 | callbacks: defined?(ActiveJob) ? :async : true |
471 | 476 | # wordnet: true |
472 | 477 | end | ... | ... |