Commit 1f5c350661f9707324be59d9189d03745feb1e3f

Authored by Andrew Kane
1 parent 45be88e1

Added index_suffix option - #891

@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 2
3 - Added `avg`, `cardinality`, `max`, `min`, and `sum` aggregations 3 - Added `avg`, `cardinality`, `max`, `min`, and `sum` aggregations
4 - Added `load: {dumpable: true}` option 4 - Added `load: {dumpable: true}` option
  5 +- Added `index_suffix` option
5 - Accept string for `exclude` option 6 - Accept string for `exclude` option
6 7
7 ## 2.2.0 8 ## 2.2.0
@@ -1770,6 +1770,14 @@ product = FactoryGirl.create(:product) @@ -1770,6 +1770,14 @@ product = FactoryGirl.create(:product)
1770 product.reindex(refresh: true) 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 ## Multi-Tenancy 1781 ## Multi-Tenancy
1774 1782
1775 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. 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.
1 require "bundler/gem_tasks" 1 require "bundler/gem_tasks"
2 require "rake/testtask" 2 require "rake/testtask"
3 3
  4 +begin
  5 + require "parallel_tests/tasks"
  6 + require "shellwords"
  7 +rescue LoadError
  8 + # do nothing
  9 +end
  10 +
4 task default: :test 11 task default: :test
5 Rake::TestTask.new do |t| 12 Rake::TestTask.new do |t|
6 t.libs << "test" 13 t.libs << "test"
lib/searchkick.rb
@@ -36,7 +36,7 @@ module Searchkick @@ -36,7 +36,7 @@ module Searchkick
36 class ImportError < Error; end 36 class ImportError < Error; end
37 37
38 class << self 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 attr_writer :client, :env, :search_timeout 40 attr_writer :client, :env, :search_timeout
41 attr_reader :aws_credentials 41 attr_reader :aws_credentials
42 end 42 end
lib/searchkick/model.rb
@@ -21,8 +21,8 @@ module Searchkick @@ -21,8 +21,8 @@ module Searchkick
21 class_variable_set :@@searchkick_klass, self 21 class_variable_set :@@searchkick_klass, self
22 class_variable_set :@@searchkick_callbacks, callbacks 22 class_variable_set :@@searchkick_callbacks, callbacks
23 class_variable_set :@@searchkick_index, options[:index_name] || 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 class << self 27 class << self
28 def searchkick_search(term = "*", **options, &block) 28 def searchkick_search(term = "*", **options, &block)
test/gemfiles/parallel_tests.gemfile 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +source 'https://rubygems.org'
  2 +
  3 +# Specify your gem's dependencies in searchkick.gemspec
  4 +gemspec path: "../../"
  5 +
  6 +gem "sqlite3"
  7 +gem "activerecord", "~> 5.0.0"
  8 +gem "parallel_tests"
test/index_test.rb
@@ -7,8 +7,9 @@ class IndexTest &lt; Minitest::Test @@ -7,8 +7,9 @@ class IndexTest &lt; Minitest::Test
7 end 7 end
8 8
9 def test_clean_indices 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 old_index.delete if old_index.exists? 14 old_index.delete if old_index.exists?
14 different_index.delete if different_index.exists? 15 different_index.delete if different_index.exists?
@@ -25,7 +26,8 @@ class IndexTest &lt; Minitest::Test @@ -25,7 +26,8 @@ class IndexTest &lt; Minitest::Test
25 end 26 end
26 27
27 def test_clean_indices_old_format 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 old_index.create 31 old_index.create
30 32
31 Product.searchkick_index.clean_indices 33 Product.searchkick_index.clean_indices
test/test_helper.rb
@@ -6,11 +6,16 @@ require &quot;logger&quot; @@ -6,11 +6,16 @@ require &quot;logger&quot;
6 require "active_support/core_ext" if defined?(NoBrainer) 6 require "active_support/core_ext" if defined?(NoBrainer)
7 require "active_support/notifications" 7 require "active_support/notifications"
8 8
  9 +Searchkick.index_suffix = ENV["TEST_ENV_NUMBER"]
  10 +
9 ENV["RACK_ENV"] = "test" 11 ENV["RACK_ENV"] = "test"
10 12
11 Minitest::Test = Minitest::Unit::TestCase unless defined?(Minitest::Test) 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 Searchkick.client.transport.logger = Logger.new("elasticsearch.log") 19 Searchkick.client.transport.logger = Logger.new("elasticsearch.log")
15 Searchkick.search_timeout = 5 20 Searchkick.search_timeout = 5
16 21
@@ -466,7 +471,7 @@ class Animal @@ -466,7 +471,7 @@ class Animal
466 searchkick \ 471 searchkick \
467 text_start: [:name], 472 text_start: [:name],
468 suggest: [:name], 473 suggest: [:name],
469 - index_name: -> { "#{name.tableize}-#{Date.today.year}" }, 474 + index_name: -> { "#{name.tableize}-#{Date.today.year}#{Searchkick.index_suffix}" },
470 callbacks: defined?(ActiveJob) ? :async : true 475 callbacks: defined?(ActiveJob) ? :async : true
471 # wordnet: true 476 # wordnet: true
472 end 477 end