Skip to content

Commit 1483ccf

Browse files
authored
Start testing & supporting MongoDB (#410)
* Configure mongodb testing * Test MongoDB in CI No special configuration here, a basic copy/paste of the example in https://github.com/marketplace/actions/mongodb-in-github-actions * Add basic MongoDB test * Make rubocop happy * Configure mongoid inline * Move Mongo test config to support file * Use after_commit instead of after_destroy_commit The bugfix on Mongoid is not released yet, but that should not matter since there is no real reason to turn this back to after_destroy_commit. The behavior is equivalent.
1 parent a35652c commit 1483ccf

File tree

8 files changed

+63
-14
lines changed

8 files changed

+63
-14
lines changed

.github/workflows/tests.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
matrix:
2121
ruby-version: ["3.2", "3.3", "3.4"]
2222
rails-version: [6.1, 7.0]
23+
mongodb-version: [8.0]
2324
env:
2425
RAILS_VERSION: ${{ matrix.rails-version }}
2526
name: integration-tests (Rails ${{ matrix.rails-version }} with Ruby ${{ matrix.ruby-version }})
@@ -34,6 +35,10 @@ jobs:
3435
bundler-cache: true
3536
- name: Meilisearch (latest) setup with Docker
3637
run: docker run -d -p 7700:7700 getmeili/meilisearch:latest meilisearch --master-key=masterKey --no-analytics
38+
- name: Start MongoDB
39+
uses: supercharge/[email protected]
40+
with:
41+
mongodb-version: ${{ matrix.mongodb-version }}
3742
- name: Run tests
3843
run: bundle exec rspec
3944
- name: Upload coverage reports to Codecov

.rubocop_todo.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2025-03-24 19:18:19 UTC using RuboCop version 1.27.0.
3+
# on 2025-03-28 20:11:18 UTC using RuboCop version 1.27.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

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

69-
# Offense count: 15
69+
# Offense count: 17
7070
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
7171
Metrics/AbcSize:
72-
Max: 105
72+
Max: 108
7373

7474
# Offense count: 1
7575
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
@@ -82,10 +82,10 @@ Metrics/BlockLength:
8282
Metrics/ClassLength:
8383
Max: 171
8484

85-
# Offense count: 11
85+
# Offense count: 12
8686
# Configuration parameters: IgnoredMethods.
8787
Metrics/CyclomaticComplexity:
88-
Max: 28
88+
Max: 29
8989

9090
# Offense count: 23
9191
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
@@ -95,12 +95,12 @@ Metrics/MethodLength:
9595
# Offense count: 1
9696
# Configuration parameters: CountComments, CountAsOne.
9797
Metrics/ModuleLength:
98-
Max: 437
98+
Max: 439
9999

100-
# Offense count: 9
100+
# Offense count: 10
101101
# Configuration parameters: IgnoredMethods.
102102
Metrics/PerceivedComplexity:
103-
Max: 35
103+
Max: 36
104104

105105
# Offense count: 1
106106
Naming/AccessorMethodName:
@@ -139,7 +139,7 @@ RSpec/ContextWording:
139139
- 'spec/options_spec.rb'
140140
- 'spec/system/tech_shop_spec.rb'
141141

142-
# Offense count: 72
142+
# Offense count: 74
143143
# Configuration parameters: CountAsOne.
144144
RSpec/ExampleLength:
145145
Max: 16
@@ -167,7 +167,7 @@ RSpec/MultipleDescribes:
167167
Exclude:
168168
- 'spec/search_spec.rb'
169169

170-
# Offense count: 3
170+
# Offense count: 5
171171
RSpec/NestedGroups:
172172
Max: 4
173173

@@ -241,7 +241,7 @@ Style/StringLiterals:
241241
Exclude:
242242
- 'spec/ms_clean_up_job_spec.rb'
243243

244-
# Offense count: 20
244+
# Offense count: 22
245245
# This cop supports safe auto-correction (--auto-correct).
246246
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
247247
# URISchemes: http, https

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ end
1515
group :test do
1616
rails_version = ENV['RAILS_VERSION'] || '7.1'
1717
sequel_version = ENV['SEQUEL_VERSION'] ? "~> #{ENV['SEQUEL_VERSION']}" : '>= 4.0'
18+
mongoid_version = ENV['MONGOID_VERSION'] || '9.0'
1819

1920
gem 'active_model_serializers'
2021
gem 'rails', "~> #{rails_version}"
2122
gem 'sequel', sequel_version
23+
gem 'mongoid', "~> #{mongoid_version}"
2224

2325
# remove when deprecate rails 6
2426
if Gem::Version.new(rails_version) >= Gem::Version.new('7.0')

lib/meilisearch-rails.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ def meilisearch(options = {}, &block)
483483
end
484484
end
485485
elsif respond_to?(:after_destroy)
486-
after_destroy_commit { |searchable| searchable.ms_enqueue_remove_from_index!(ms_synchronous?) }
486+
after_commit(on: :destroy) { |searchable| searchable.ms_enqueue_remove_from_index!(ms_synchronous?) }
487487
end
488488
end
489489
end
@@ -696,6 +696,8 @@ def ms_search(query, params = {})
696696
# respond with a valid database column. The blocks below prevent that from happening.
697697
has_virtual_column_as_pk = if defined?(::Sequel::Model) && self < Sequel::Model
698698
meilisearch_options[:type].columns.map(&:to_s).exclude?(condition_key.to_s)
699+
elsif Utilities.mongo_model?(self)
700+
fields.keys.exclude?(condition_key.to_s)
699701
else
700702
meilisearch_options[:type].columns.map(&:name).map(&:to_s).exclude?(condition_key.to_s)
701703
end
@@ -784,7 +786,7 @@ def ms_must_reindex?(document)
784786

785787
def ms_primary_key_method(options = nil)
786788
options ||= meilisearch_options
787-
options[:primary_key] || options[:id] || :id
789+
options[:primary_key] || options[:id] || (Utilities.mongo_model?(self) ? :_id : :id)
788790
end
789791

790792
protected
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'support/mongo_models/citizen'
2+
3+
describe 'MongoDB record is created' do
4+
it 'is added to meilisearch' do
5+
john_wick = Citizen.create(name: 'John Wick', age: 40)
6+
7+
AsyncHelper.await_last_task
8+
9+
expect(Citizen.search('John')).to eq([john_wick])
10+
end
11+
end

spec/spec_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
require 'sqlite3' unless defined?(JRUBY_VERSION)
3232
require 'logger'
3333
require 'sequel'
34+
require 'mongoid'
3435
require 'active_model_serializers'
3536
require 'byebug'
3637

@@ -67,5 +68,7 @@
6768
safe_index_list.each do |index|
6869
Meilisearch::Rails.client.delete_index(index)
6970
end
71+
72+
Mongoid.default_client.database.drop
7073
end
7174
end

spec/support/mongo_models/citizen.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Citizen
2+
include Mongoid::Document
3+
include Mongoid::Timestamps
4+
5+
field :name, type: String
6+
field :age, type: Integer
7+
8+
include MeiliSearch::Rails
9+
10+
meilisearch index_uid: safe_index_uid('Citizen')
11+
end

spec/support/mongoid_config.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
mongo_db_name = (ENV['MONGODB_DATABASE'] || '_ms_rails_test') + SecureRandom.hex(8)
2+
Mongoid.load_configuration(
3+
{
4+
clients: {
5+
default: {
6+
database: mongo_db_name,
7+
hosts: [ENV['MONGODB_HOST'] || 'localhost:27017'],
8+
options: {
9+
read: { mode: :primary },
10+
max_pool_size: 1
11+
}
12+
}
13+
}
14+
}
15+
)

0 commit comments

Comments
 (0)