diff --git a/CHANGELOG.md b/CHANGELOG.md index fa97eaf..745e74d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Added `avg`, `cardinality`, `max`, `min`, and `sum` aggregations - Added `load: {dumpable: true}` option +- Added `index_suffix` option - Accept string for `exclude` option ## 2.2.0 diff --git a/README.md b/README.md index ba90c4d..b006c15 100644 --- a/README.md +++ b/README.md @@ -1770,6 +1770,14 @@ product = FactoryGirl.create(:product) product.reindex(refresh: true) ``` +### Parallel Tests + +Set: + +```ruby +Searchkick.index_suffix = ENV["TEST_ENV_NUMBER"] +``` + ## Multi-Tenancy 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. diff --git a/Rakefile b/Rakefile index 93c1699..8c215e6 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,13 @@ require "bundler/gem_tasks" require "rake/testtask" +begin + require "parallel_tests/tasks" + require "shellwords" +rescue LoadError + # do nothing +end + task default: :test Rake::TestTask.new do |t| t.libs << "test" diff --git a/lib/searchkick.rb b/lib/searchkick.rb index b73703a..391cab3 100644 --- a/lib/searchkick.rb +++ b/lib/searchkick.rb @@ -36,7 +36,7 @@ module Searchkick class ImportError < Error; end class << self - attr_accessor :search_method_name, :wordnet_path, :timeout, :models, :client_options, :redis + attr_accessor :search_method_name, :wordnet_path, :timeout, :models, :client_options, :redis, :index_suffix attr_writer :client, :env, :search_timeout attr_reader :aws_credentials end diff --git a/lib/searchkick/model.rb b/lib/searchkick/model.rb index bc936af..ebe5b74 100644 --- a/lib/searchkick/model.rb +++ b/lib/searchkick/model.rb @@ -21,8 +21,8 @@ module Searchkick class_variable_set :@@searchkick_klass, self class_variable_set :@@searchkick_callbacks, callbacks class_variable_set :@@searchkick_index, options[:index_name] || - (options[:index_prefix].respond_to?(:call) && proc { [options[:index_prefix].call, model_name.plural, Searchkick.env].compact.join("_") }) || - [options[:index_prefix], model_name.plural, Searchkick.env].compact.join("_") + (options[:index_prefix].respond_to?(:call) && proc { [options[:index_prefix].call, model_name.plural, Searchkick.env, Searchkick.index_suffix].compact.join("_") }) || + [options[:index_prefix], model_name.plural, Searchkick.env, Searchkick.index_suffix].compact.join("_") class << self def searchkick_search(term = "*", **options, &block) diff --git a/test/gemfiles/parallel_tests.gemfile b/test/gemfiles/parallel_tests.gemfile new file mode 100644 index 0000000..73ba4b0 --- /dev/null +++ b/test/gemfiles/parallel_tests.gemfile @@ -0,0 +1,8 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in searchkick.gemspec +gemspec path: "../../" + +gem "sqlite3" +gem "activerecord", "~> 5.0.0" +gem "parallel_tests" diff --git a/test/index_test.rb b/test/index_test.rb index 13e01eb..f1397a9 100644 --- a/test/index_test.rb +++ b/test/index_test.rb @@ -7,8 +7,9 @@ class IndexTest < Minitest::Test end def test_clean_indices - old_index = Searchkick::Index.new("products_test_20130801000000000") - different_index = Searchkick::Index.new("items_test_20130801000000000") + suffix = Searchkick.index_suffix ? "_#{Searchkick.index_suffix}" : "" + old_index = Searchkick::Index.new("products_test#{suffix}_20130801000000000") + different_index = Searchkick::Index.new("items_test#{suffix}_20130801000000000") old_index.delete if old_index.exists? different_index.delete if different_index.exists? @@ -25,7 +26,8 @@ class IndexTest < Minitest::Test end def test_clean_indices_old_format - old_index = Searchkick::Index.new("products_test_20130801000000") + suffix = Searchkick.index_suffix ? "_#{Searchkick.index_suffix}" : "" + old_index = Searchkick::Index.new("products_test#{suffix}_20130801000000") old_index.create Product.searchkick_index.clean_indices diff --git a/test/test_helper.rb b/test/test_helper.rb index 82e378e..c151d6b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,11 +6,16 @@ require "logger" require "active_support/core_ext" if defined?(NoBrainer) require "active_support/notifications" +Searchkick.index_suffix = ENV["TEST_ENV_NUMBER"] + ENV["RACK_ENV"] = "test" Minitest::Test = Minitest::Unit::TestCase unless defined?(Minitest::Test) -File.delete("elasticsearch.log") if File.exist?("elasticsearch.log") +if !defined?(ParallelTests) || ParallelTests.first_process? + File.delete("elasticsearch.log") if File.exist?("elasticsearch.log") +end + Searchkick.client.transport.logger = Logger.new("elasticsearch.log") Searchkick.search_timeout = 5 @@ -466,7 +471,7 @@ class Animal searchkick \ text_start: [:name], suggest: [:name], - index_name: -> { "#{name.tableize}-#{Date.today.year}" }, + index_name: -> { "#{name.tableize}-#{Date.today.year}#{Searchkick.index_suffix}" }, callbacks: defined?(ActiveJob) ? :async : true # wordnet: true end -- libgit2 0.21.0