Skip to content

Start testing & supporting MongoDB #410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
matrix:
ruby-version: ["3.2", "3.3", "3.4"]
rails-version: [6.1, 7.0]
mongodb-version: [8.0]
env:
RAILS_VERSION: ${{ matrix.rails-version }}
name: integration-tests (Rails ${{ matrix.rails-version }} with Ruby ${{ matrix.ruby-version }})
Expand All @@ -34,6 +35,10 @@ jobs:
bundler-cache: true
- name: Meilisearch (latest) setup with Docker
run: docker run -d -p 7700:7700 getmeili/meilisearch:latest meilisearch --master-key=masterKey --no-analytics
- name: Start MongoDB
uses: supercharge/[email protected]
with:
mongodb-version: ${{ matrix.mongodb-version }}
- name: Run tests
run: bundle exec rspec
- name: Upload coverage reports to Codecov
Expand Down
24 changes: 12 additions & 12 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2025-03-24 19:18:19 UTC using RuboCop version 1.27.0.
# on 2025-03-28 20:11:18 UTC using RuboCop version 1.27.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Offense count: 2
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: TreatCommentsAsGroupSeparators, ConsiderPunctuation, Include.
# Include: **/*.gemfile, **/Gemfile, **/gems.rb
Expand Down Expand Up @@ -66,10 +66,10 @@ Lint/UnusedMethodArgument:
Exclude:
- 'lib/meilisearch-rails.rb'

# Offense count: 15
# Offense count: 17
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 105
Max: 108

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Expand All @@ -82,10 +82,10 @@ Metrics/BlockLength:
Metrics/ClassLength:
Max: 171

# Offense count: 11
# Offense count: 12
# Configuration parameters: IgnoredMethods.
Metrics/CyclomaticComplexity:
Max: 28
Max: 29

# Offense count: 23
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Expand All @@ -95,12 +95,12 @@ Metrics/MethodLength:
# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ModuleLength:
Max: 437
Max: 439

# Offense count: 9
# Offense count: 10
# Configuration parameters: IgnoredMethods.
Metrics/PerceivedComplexity:
Max: 35
Max: 36

# Offense count: 1
Naming/AccessorMethodName:
Expand Down Expand Up @@ -139,7 +139,7 @@ RSpec/ContextWording:
- 'spec/options_spec.rb'
- 'spec/system/tech_shop_spec.rb'

# Offense count: 72
# Offense count: 74
# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 16
Expand Down Expand Up @@ -167,7 +167,7 @@ RSpec/MultipleDescribes:
Exclude:
- 'spec/search_spec.rb'

# Offense count: 3
# Offense count: 5
RSpec/NestedGroups:
Max: 4

Expand Down Expand Up @@ -241,7 +241,7 @@ Style/StringLiterals:
Exclude:
- 'spec/ms_clean_up_job_spec.rb'

# Offense count: 20
# Offense count: 22
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ end
group :test do
rails_version = ENV['RAILS_VERSION'] || '7.1'
sequel_version = ENV['SEQUEL_VERSION'] ? "~> #{ENV['SEQUEL_VERSION']}" : '>= 4.0'
mongoid_version = ENV['MONGOID_VERSION'] || '9.0'

gem 'active_model_serializers'
gem 'rails', "~> #{rails_version}"
gem 'sequel', sequel_version
gem 'mongoid', "~> #{mongoid_version}"

# remove when deprecate rails 6
if Gem::Version.new(rails_version) >= Gem::Version.new('7.0')
Expand Down
6 changes: 4 additions & 2 deletions lib/meilisearch-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def meilisearch(options = {}, &block)
end
end
elsif respond_to?(:after_destroy)
after_destroy_commit { |searchable| searchable.ms_enqueue_remove_from_index!(ms_synchronous?) }
after_commit(on: :destroy) { |searchable| searchable.ms_enqueue_remove_from_index!(ms_synchronous?) }
end
end
end
Expand Down Expand Up @@ -696,6 +696,8 @@ def ms_search(query, params = {})
# respond with a valid database column. The blocks below prevent that from happening.
has_virtual_column_as_pk = if defined?(::Sequel::Model) && self < Sequel::Model
meilisearch_options[:type].columns.map(&:to_s).exclude?(condition_key.to_s)
elsif Utilities.mongo_model?(self)
fields.keys.exclude?(condition_key.to_s)
else
meilisearch_options[:type].columns.map(&:name).map(&:to_s).exclude?(condition_key.to_s)
end
Expand Down Expand Up @@ -784,7 +786,7 @@ def ms_must_reindex?(document)

def ms_primary_key_method(options = nil)
options ||= meilisearch_options
options[:primary_key] || options[:id] || :id
options[:primary_key] || options[:id] || (Utilities.mongo_model?(self) ? :_id : :id)
end

protected
Expand Down
11 changes: 11 additions & 0 deletions spec/integration/mongoid/record_is_created_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'support/mongo_models/citizen'

describe 'MongoDB record is created' do
it 'is added to meilisearch' do
john_wick = Citizen.create(name: 'John Wick', age: 40)

AsyncHelper.await_last_task

expect(Citizen.search('John')).to eq([john_wick])
end
end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
require 'sqlite3' unless defined?(JRUBY_VERSION)
require 'logger'
require 'sequel'
require 'mongoid'
require 'active_model_serializers'
require 'byebug'

Expand Down Expand Up @@ -67,5 +68,7 @@
safe_index_list.each do |index|
Meilisearch::Rails.client.delete_index(index)
end

Mongoid.default_client.database.drop
end
end
11 changes: 11 additions & 0 deletions spec/support/mongo_models/citizen.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Citizen
include Mongoid::Document
include Mongoid::Timestamps

field :name, type: String
field :age, type: Integer

include MeiliSearch::Rails

meilisearch index_uid: safe_index_uid('Citizen')
end
15 changes: 15 additions & 0 deletions spec/support/mongoid_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mongo_db_name = (ENV['MONGODB_DATABASE'] || '_ms_rails_test') + SecureRandom.hex(8)
Mongoid.load_configuration(
{
clients: {
default: {
database: mongo_db_name,
hosts: [ENV['MONGODB_HOST'] || 'localhost:27017'],
options: {
read: { mode: :primary },
max_pool_size: 1
}
}
}
}
)