mongoid 7.1.0.rc0 → 7.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/CHANGELOG.md +6 -6
- data/LICENSE +1 -1
- data/README.md +5 -5
- data/Rakefile +14 -0
- data/lib/config/locales/en.yml +5 -5
- data/lib/mongoid.rb +3 -2
- data/lib/mongoid/association/accessors.rb +37 -2
- data/lib/mongoid/association/embedded/embeds_many.rb +2 -1
- data/lib/mongoid/association/embedded/embeds_one.rb +2 -1
- data/lib/mongoid/association/many.rb +3 -2
- data/lib/mongoid/association/proxy.rb +6 -4
- data/lib/mongoid/association/referenced/belongs_to/binding.rb +1 -1
- data/lib/mongoid/association/referenced/belongs_to/eager.rb +38 -2
- data/lib/mongoid/association/referenced/eager.rb +29 -9
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +2 -22
- data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -2
- data/lib/mongoid/atomic.rb +13 -3
- data/lib/mongoid/attributes.rb +28 -20
- data/lib/mongoid/clients/factory.rb +2 -2
- data/lib/mongoid/clients/options.rb +8 -8
- data/lib/mongoid/clients/sessions.rb +20 -4
- data/lib/mongoid/clients/storage_options.rb +5 -5
- data/lib/mongoid/config.rb +42 -12
- data/lib/mongoid/config/options.rb +5 -2
- data/lib/mongoid/contextual.rb +5 -4
- data/lib/mongoid/contextual/geo_near.rb +3 -2
- data/lib/mongoid/contextual/map_reduce.rb +3 -2
- data/lib/mongoid/contextual/mongo.rb +2 -1
- data/lib/mongoid/criteria.rb +23 -4
- data/lib/mongoid/criteria/modifiable.rb +2 -1
- data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/regexp.rb +6 -6
- data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +12 -0
- data/lib/mongoid/criteria/queryable/mergeable.rb +75 -8
- data/lib/mongoid/criteria/queryable/pipeline.rb +3 -2
- data/lib/mongoid/criteria/queryable/selectable.rb +120 -13
- data/lib/mongoid/criteria/queryable/storable.rb +104 -99
- data/lib/mongoid/errors/eager_load.rb +2 -0
- data/lib/mongoid/errors/no_client_config.rb +2 -2
- data/lib/mongoid/errors/no_default_client.rb +1 -1
- data/lib/mongoid/extensions/hash.rb +4 -2
- data/lib/mongoid/extensions/regexp.rb +1 -1
- data/lib/mongoid/fields.rb +2 -1
- data/lib/mongoid/fields/standard.rb +2 -1
- data/lib/mongoid/fields/validators/macro.rb +4 -1
- data/lib/mongoid/findable.rb +5 -4
- data/lib/mongoid/interceptable.rb +5 -1
- data/lib/mongoid/matchable/regexp.rb +2 -2
- data/lib/mongoid/persistable/pushable.rb +11 -2
- data/lib/mongoid/persistence_context.rb +6 -6
- data/lib/mongoid/query_cache.rb +61 -18
- data/lib/mongoid/railties/database.rake +7 -0
- data/lib/mongoid/serializable.rb +10 -2
- data/lib/mongoid/shardable.rb +56 -4
- data/lib/mongoid/tasks/database.rake +10 -5
- data/lib/mongoid/tasks/database.rb +83 -0
- data/lib/mongoid/timestamps/timeless.rb +3 -1
- data/lib/mongoid/validatable/uniqueness.rb +1 -1
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +32 -23
- data/lib/rails/generators/mongoid/model/templates/model.rb.tt +1 -1
- data/spec/app/models/coding.rb +4 -0
- data/spec/app/models/coding/pull_request.rb +12 -0
- data/spec/app/models/delegating_patient.rb +16 -0
- data/spec/app/models/passport.rb +1 -0
- data/spec/app/models/phone.rb +1 -0
- data/spec/app/models/publication.rb +5 -0
- data/spec/app/models/publication/encyclopedia.rb +12 -0
- data/spec/app/models/publication/review.rb +14 -0
- data/spec/integration/app_spec.rb +254 -0
- data/spec/integration/associations/embedded_spec.rb +54 -0
- data/spec/integration/associations/has_many_spec.rb +34 -0
- data/spec/integration/associations/has_one_spec.rb +34 -0
- data/spec/integration/bson_regexp_raw_spec.rb +20 -0
- data/spec/integration/criteria/date_field_spec.rb +41 -0
- data/spec/integration/criteria/logical_spec.rb +13 -0
- data/spec/integration/document_spec.rb +22 -0
- data/spec/integration/shardable_spec.rb +149 -0
- data/spec/lite_spec_helper.rb +15 -4
- data/spec/mongoid/association/accessors_spec.rb +238 -63
- data/spec/mongoid/association/embedded/embeds_many_models.rb +19 -0
- data/spec/mongoid/association/embedded/embeds_many_spec.rb +10 -0
- data/spec/mongoid/association/embedded/embeds_one_spec.rb +0 -2
- data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +193 -10
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +140 -1
- data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +146 -68
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +2 -1
- data/spec/mongoid/attributes_spec.rb +19 -7
- data/spec/mongoid/changeable_spec.rb +23 -0
- data/spec/mongoid/clients/factory_spec.rb +8 -8
- data/spec/mongoid/clients/options_spec.rb +11 -11
- data/spec/mongoid/clients/sessions_spec.rb +8 -4
- data/spec/mongoid/clients/transactions_spec.rb +20 -8
- data/spec/mongoid/clients_spec.rb +2 -2
- data/spec/mongoid/contextual/atomic_spec.rb +22 -11
- data/spec/mongoid/contextual/geo_near_spec.rb +11 -2
- data/spec/mongoid/contextual/map_reduce_spec.rb +20 -5
- data/spec/mongoid/contextual/mongo_spec.rb +76 -53
- data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +1 -1
- data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +7 -7
- data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +1 -1
- data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +19 -7
- data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +28 -1
- data/spec/mongoid/criteria/queryable/mergeable_spec.rb +45 -12
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +1051 -392
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +52 -0
- data/spec/mongoid/criteria/queryable/storable_spec.rb +80 -2
- data/spec/mongoid/criteria_spec.rb +36 -2
- data/spec/mongoid/document_fields_spec.rb +29 -0
- data/spec/mongoid/document_persistence_context_spec.rb +33 -0
- data/spec/mongoid/errors/no_client_config_spec.rb +2 -2
- data/spec/mongoid/errors/no_client_database_spec.rb +3 -3
- data/spec/mongoid/errors/no_client_hosts_spec.rb +3 -3
- data/spec/mongoid/fields_spec.rb +24 -1
- data/spec/mongoid/indexable_spec.rb +6 -4
- data/spec/mongoid/interceptable_spec.rb +62 -0
- data/spec/mongoid/interceptable_spec_models.rb +76 -0
- data/spec/mongoid/matchable/default_spec.rb +1 -1
- data/spec/mongoid/matchable/regexp_spec.rb +2 -2
- data/spec/mongoid/matchable_spec.rb +2 -2
- data/spec/mongoid/persistable/pushable_spec.rb +55 -1
- data/spec/mongoid/query_cache_spec.rb +77 -9
- data/spec/mongoid/relations/proxy_spec.rb +1 -1
- data/spec/mongoid/scopable_spec.rb +2 -1
- data/spec/mongoid/serializable_spec.rb +129 -18
- data/spec/mongoid/shardable_models.rb +61 -0
- data/spec/mongoid/shardable_spec.rb +69 -16
- data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
- data/spec/mongoid/tasks/database_spec.rb +1 -1
- data/spec/spec_helper.rb +2 -31
- data/spec/support/child_process_helper.rb +76 -0
- data/spec/support/cluster_config.rb +3 -3
- data/spec/support/constraints.rb +26 -10
- data/spec/support/expectations.rb +3 -1
- data/spec/support/helpers.rb +11 -0
- data/spec/support/lite_constraints.rb +22 -0
- data/spec/support/session_registry.rb +50 -0
- data/spec/support/spec_config.rb +12 -4
- metadata +518 -480
- metadata.gz.sig +0 -0
@@ -11,9 +11,9 @@ module Mongoid
|
|
11
11
|
# Create the new error.
|
12
12
|
#
|
13
13
|
# @example Create the error.
|
14
|
-
# NoClientConfig.new(:
|
14
|
+
# NoClientConfig.new(:analytics)
|
15
15
|
#
|
16
|
-
# @param [ String
|
16
|
+
# @param [ String | Symbol ] name The name of the client.
|
17
17
|
#
|
18
18
|
# @since 3.0.0
|
19
19
|
def initialize(name)
|
@@ -10,7 +10,7 @@ module Mongoid
|
|
10
10
|
# Create the new error with the defined client names.
|
11
11
|
#
|
12
12
|
# @example Create the new error.
|
13
|
-
# NoDefaultClient.new([ :
|
13
|
+
# NoDefaultClient.new([ :analytics ])
|
14
14
|
#
|
15
15
|
# @param [ Array<Symbol> ] keys The defined clients.
|
16
16
|
#
|
@@ -48,9 +48,11 @@ module Mongoid
|
|
48
48
|
value.each_pair do |_key, _value|
|
49
49
|
value[_key] = (key == "$rename") ? _value.to_s : mongoize_for(key, klass, _key, _value)
|
50
50
|
end
|
51
|
-
|
51
|
+
consolidated[key] ||= {}
|
52
|
+
consolidated[key].update(value)
|
52
53
|
else
|
53
|
-
|
54
|
+
consolidated["$set"] ||= {}
|
55
|
+
consolidated["$set"].update(key => mongoize_for(key, klass, key, value))
|
54
56
|
end
|
55
57
|
end
|
56
58
|
consolidated
|
data/lib/mongoid/fields.rb
CHANGED
@@ -500,7 +500,8 @@ module Mongoid
|
|
500
500
|
def create_translations_getter(name, meth)
|
501
501
|
generated_methods.module_eval do
|
502
502
|
re_define_method("#{meth}_translations") do
|
503
|
-
|
503
|
+
attributes[name] ||= {}
|
504
|
+
attributes[name].with_indifferent_access
|
504
505
|
end
|
505
506
|
alias_method :"#{meth}_t", :"#{meth}_translations"
|
506
507
|
end
|
@@ -4,12 +4,13 @@
|
|
4
4
|
module Mongoid
|
5
5
|
module Fields
|
6
6
|
class Standard
|
7
|
+
extend Forwardable
|
7
8
|
|
8
9
|
# Defines the behavior for defined fields in the document.
|
9
10
|
# Set readers for the instance variables.
|
10
11
|
attr_accessor :default_val, :label, :name, :options
|
11
12
|
|
12
|
-
|
13
|
+
def_delegators :type, :demongoize, :evolve, :mongoize
|
13
14
|
|
14
15
|
# Adds the atomic changes for this type of resizable field.
|
15
16
|
#
|
@@ -113,7 +113,10 @@ module Mongoid
|
|
113
113
|
end
|
114
114
|
|
115
115
|
if option == :type && options[option] == Symbol
|
116
|
-
|
116
|
+
@field_type_is_symbol_warned ||= begin
|
117
|
+
Mongoid.logger.warn(FIELD_TYPE_IS_SYMBOL)
|
118
|
+
true
|
119
|
+
end
|
117
120
|
end
|
118
121
|
end
|
119
122
|
end
|
data/lib/mongoid/findable.rb
CHANGED
@@ -8,15 +8,16 @@ module Mongoid
|
|
8
8
|
#
|
9
9
|
# @since 4.0.0
|
10
10
|
module Findable
|
11
|
+
extend Forwardable
|
11
12
|
|
12
|
-
|
13
|
+
def_delegators :with_default_scope, *(
|
13
14
|
Criteria::Queryable::Selectable.forwardables +
|
14
15
|
Criteria::Queryable::Optional.forwardables
|
15
|
-
)
|
16
|
+
)
|
16
17
|
|
17
18
|
# These are methods defined on the criteria that should also be accessible
|
18
19
|
# directly from the class level.
|
19
|
-
|
20
|
+
def_delegators :with_default_scope,
|
20
21
|
:aggregates,
|
21
22
|
:avg,
|
22
23
|
:create_with,
|
@@ -45,7 +46,7 @@ module Mongoid
|
|
45
46
|
:sum,
|
46
47
|
:text_search,
|
47
48
|
:update,
|
48
|
-
:update_all
|
49
|
+
:update_all
|
49
50
|
|
50
51
|
# Returns a count of records in the database.
|
51
52
|
# If you want to specify conditions use where.
|
@@ -10,8 +10,8 @@ module Mongoid
|
|
10
10
|
# Does the supplied query match the attribute?
|
11
11
|
#
|
12
12
|
# @example Does this match?
|
13
|
-
# matcher._matches?(
|
14
|
-
# matcher._matches?(BSON::Regex::Raw.new("
|
13
|
+
# matcher._matches?(/\AEm/)
|
14
|
+
# matcher._matches?(BSON::Regex::Raw.new("\\AEm"))
|
15
15
|
#
|
16
16
|
# @param [ BSON::Regexp::Raw, Regexp ] regexp The regular expression object.
|
17
17
|
#
|
@@ -24,7 +24,13 @@ module Mongoid
|
|
24
24
|
def add_to_set(adds)
|
25
25
|
prepare_atomic_operation do |ops|
|
26
26
|
process_atomic_operations(adds) do |field, value|
|
27
|
-
existing = send(field) ||
|
27
|
+
existing = send(field) || attributes[field]
|
28
|
+
if existing.nil?
|
29
|
+
attributes[field] = []
|
30
|
+
# Read the value out of attributes:
|
31
|
+
# https://jira.mongodb.org/browse/MONGOID-4874
|
32
|
+
existing = attributes[field]
|
33
|
+
end
|
28
34
|
values = [ value ].flatten(1)
|
29
35
|
values.each do |val|
|
30
36
|
existing.push(val) unless existing.include?(val)
|
@@ -51,7 +57,10 @@ module Mongoid
|
|
51
57
|
def push(pushes)
|
52
58
|
prepare_atomic_operation do |ops|
|
53
59
|
process_atomic_operations(pushes) do |field, value|
|
54
|
-
existing = send(field) ||
|
60
|
+
existing = send(field) || begin
|
61
|
+
attributes[field] ||= []
|
62
|
+
attributes[field]
|
63
|
+
end
|
55
64
|
values = [ value ].flatten(1)
|
56
65
|
values.each{ |val| existing.push(val) }
|
57
66
|
ops[atomic_attribute_name(field)] = { "$each" => values }
|
@@ -122,6 +122,12 @@ module Mongoid
|
|
122
122
|
end
|
123
123
|
end
|
124
124
|
|
125
|
+
def client_name
|
126
|
+
@client_name ||= options[:client] ||
|
127
|
+
Threaded.client_override ||
|
128
|
+
storage_options && __evaluate__(storage_options[:client])
|
129
|
+
end
|
130
|
+
|
125
131
|
# Determine if this persistence context is equal to another.
|
126
132
|
#
|
127
133
|
# @example Compare two persistence contexts.
|
@@ -139,12 +145,6 @@ module Mongoid
|
|
139
145
|
|
140
146
|
private
|
141
147
|
|
142
|
-
def client_name
|
143
|
-
@client_name ||= options[:client] ||
|
144
|
-
Threaded.client_override ||
|
145
|
-
storage_options && __evaluate__(storage_options[:client])
|
146
|
-
end
|
147
|
-
|
148
148
|
def set_options!(opts)
|
149
149
|
@options ||= opts.each.reduce({}) do |_options, (key, value)|
|
150
150
|
unless VALID_OPTIONS.include?(key.to_sym)
|
data/lib/mongoid/query_cache.rb
CHANGED
@@ -163,13 +163,13 @@ module Mongoid
|
|
163
163
|
private
|
164
164
|
|
165
165
|
def process(result)
|
166
|
-
|
167
|
-
|
168
|
-
@coll_name ||= result.namespace.sub("#{database.name}.", '') if result.namespace
|
169
|
-
documents = result.documents
|
166
|
+
documents = super
|
167
|
+
|
170
168
|
if @cursor_id.zero? && !@after_first_batch
|
171
|
-
|
169
|
+
@cached_documents ||= []
|
170
|
+
@cached_documents.concat(documents)
|
172
171
|
end
|
172
|
+
|
173
173
|
@after_first_batch = true
|
174
174
|
documents
|
175
175
|
end
|
@@ -223,20 +223,40 @@ module Mongoid
|
|
223
223
|
#
|
224
224
|
# @since 5.0.0
|
225
225
|
def each
|
226
|
-
if system_collection? || !QueryCache.enabled?
|
226
|
+
if system_collection? || !QueryCache.enabled? || (respond_to?(:write?, true) && write?)
|
227
227
|
super
|
228
228
|
else
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
229
|
+
@cursor = nil
|
230
|
+
unless @cursor = cached_cursor
|
231
|
+
|
232
|
+
if driver_supports_cursor_sessions?
|
233
|
+
session = client.send(:get_session, @options)
|
234
|
+
read_with_retry(session, server_selector) do |server|
|
235
|
+
result = send_initial_query(server, session)
|
236
|
+
@cursor = get_cursor(result, server, session)
|
237
|
+
end
|
238
|
+
else
|
239
|
+
read_with_retry do
|
240
|
+
server = server_selector.select_server(cluster)
|
241
|
+
result = send_initial_query(server)
|
242
|
+
@cursor = get_cursor(result, server)
|
243
|
+
end
|
234
244
|
end
|
235
245
|
end
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
246
|
+
|
247
|
+
if block_given?
|
248
|
+
if limit && limit != -1
|
249
|
+
@cursor.to_a[0...limit].each do |doc|
|
250
|
+
yield doc
|
251
|
+
end
|
252
|
+
else
|
253
|
+
@cursor.each do |doc|
|
254
|
+
yield doc
|
255
|
+
end
|
256
|
+
end
|
257
|
+
else
|
258
|
+
@cursor.to_enum
|
259
|
+
end
|
240
260
|
end
|
241
261
|
end
|
242
262
|
|
@@ -246,13 +266,30 @@ module Mongoid
|
|
246
266
|
if limit
|
247
267
|
key = [ collection.namespace, selector, nil, skip, sort, projection, collation ]
|
248
268
|
cursor = QueryCache.cache_table[key]
|
249
|
-
if cursor
|
250
|
-
cursor.to_a[0...limit.abs]
|
251
|
-
end
|
252
269
|
end
|
253
270
|
cursor || QueryCache.cache_table[cache_key]
|
254
271
|
end
|
255
272
|
|
273
|
+
def get_cursor(result, server, session = nil)
|
274
|
+
if result.cursor_id == 0 || result.cursor_id.nil?
|
275
|
+
cursor = if session
|
276
|
+
CachedCursor.new(view, result, server, session: session)
|
277
|
+
else
|
278
|
+
CachedCursor.new(view, result, server)
|
279
|
+
end
|
280
|
+
|
281
|
+
QueryCache.cache_table[cache_key] = cursor
|
282
|
+
else
|
283
|
+
cursor = if session
|
284
|
+
Mongo::Cursor.new(view, result, server, session: session)
|
285
|
+
else
|
286
|
+
Mongo::Cursor.new(view, result, server)
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
cursor
|
291
|
+
end
|
292
|
+
|
256
293
|
def cache_key
|
257
294
|
[ collection.namespace, selector, limit, skip, sort, projection, collation ]
|
258
295
|
end
|
@@ -260,6 +297,12 @@ module Mongoid
|
|
260
297
|
def system_collection?
|
261
298
|
collection.namespace =~ /\Asystem./
|
262
299
|
end
|
300
|
+
|
301
|
+
def driver_supports_cursor_sessions?
|
302
|
+
# Driver versions 2.9 and newer support passing in a session to the
|
303
|
+
# cursor object.
|
304
|
+
(Mongo::VERSION.split('.').map(&:to_i) <=> [2, 9, 0]) > 0
|
305
|
+
end
|
263
306
|
end
|
264
307
|
|
265
308
|
# Adds behavior to the query cache for collections.
|
@@ -60,13 +60,20 @@ namespace :db do
|
|
60
60
|
end
|
61
61
|
|
62
62
|
unless Rake::Task.task_defined?("db:create_indexes")
|
63
|
+
desc "Create indexes specified in Mongoid models"
|
63
64
|
task :create_indexes => "mongoid:create_indexes"
|
64
65
|
end
|
65
66
|
|
66
67
|
unless Rake::Task.task_defined?("db:remove_indexes")
|
68
|
+
desc "Remove indexes specified in Mongoid models"
|
67
69
|
task :remove_indexes => "mongoid:remove_indexes"
|
68
70
|
end
|
69
71
|
|
72
|
+
unless Rake::Task.task_defined?("db:shard_collections")
|
73
|
+
desc "Shard collections with shard keys specified in Mongoid models"
|
74
|
+
task :shard_collections => "mongoid:shard_collections"
|
75
|
+
end
|
76
|
+
|
70
77
|
namespace :mongoid do
|
71
78
|
task :load_models do
|
72
79
|
::Rails.application.eager_load! if defined?(::Rails)
|
data/lib/mongoid/serializable.rb
CHANGED
@@ -13,8 +13,16 @@ module Mongoid
|
|
13
13
|
# We need to redefine where the JSON configuration is getting defined,
|
14
14
|
# similar to +ActiveRecord+.
|
15
15
|
included do
|
16
|
-
|
17
|
-
|
16
|
+
|
17
|
+
class << self
|
18
|
+
# Note that this intentionally only delegates :include_root_in_json
|
19
|
+
# and not :include_root_in_json? - delegating the latter produces
|
20
|
+
# wrong behavior.
|
21
|
+
# Also note that this intentionally uses the ActiveSupport delegation
|
22
|
+
# functionality and not the Ruby standard library one.
|
23
|
+
# See https://jira.mongodb.org/browse/MONGOID-4849.
|
24
|
+
delegate :include_root_in_json, to: ::Mongoid
|
25
|
+
end
|
18
26
|
end
|
19
27
|
|
20
28
|
# Gets the document as a serializable hash, used by ActiveModel's JSON
|
data/lib/mongoid/shardable.rb
CHANGED
@@ -10,8 +10,32 @@ module Mongoid
|
|
10
10
|
extend ActiveSupport::Concern
|
11
11
|
|
12
12
|
included do
|
13
|
+
# Returns the list of shard key fields, if shard key was declared on
|
14
|
+
# this model. If no shard key was declared, returns an empty array.
|
15
|
+
#
|
16
|
+
# @return [ Array<Symbol> ] List of shard key fields.
|
17
|
+
# @api public
|
13
18
|
cattr_accessor :shard_key_fields
|
14
19
|
self.shard_key_fields = []
|
20
|
+
|
21
|
+
# Returns the shard configuration, which is a hash with the following
|
22
|
+
# (symbol) keys:
|
23
|
+
#
|
24
|
+
# - keys: A hash mapping (symbol) field names to values, defining the
|
25
|
+
# shard key. Values can be either the integer 1 for ranged sharding
|
26
|
+
# or the string "hashed" for hashed sharding.
|
27
|
+
# - options: A hash containing options for shardCollections command.
|
28
|
+
#
|
29
|
+
# If shard key was not declared via the +shard_key+ macro, +shard_config+
|
30
|
+
# attribute is nil.
|
31
|
+
#
|
32
|
+
# @example Get the shard configuration.
|
33
|
+
# Model.shard_config
|
34
|
+
# # => {key: {foo: 1, bar: 1}, options: {unique: true}}
|
35
|
+
#
|
36
|
+
# @return [ Hash | nil ] Shard configuration.
|
37
|
+
# @api public
|
38
|
+
cattr_accessor :shard_config
|
15
39
|
end
|
16
40
|
|
17
41
|
# Get the shard key fields.
|
@@ -55,14 +79,42 @@ module Mongoid
|
|
55
79
|
# field :first_name, :type => String
|
56
80
|
# field :last_name, :type => String
|
57
81
|
#
|
58
|
-
# shard_key :
|
82
|
+
# shard_key first_name: 1, last_name: 1
|
59
83
|
# end
|
60
84
|
#
|
61
85
|
# @since 2.0.0
|
62
|
-
def shard_key(*
|
63
|
-
|
64
|
-
|
86
|
+
def shard_key(*args)
|
87
|
+
unless args.first.is_a?(Hash)
|
88
|
+
# Shorthand syntax
|
89
|
+
if args.last.is_a?(Hash)
|
90
|
+
raise ArgumentError, 'Shorthand shard_key syntax does not permit options'
|
91
|
+
end
|
92
|
+
|
93
|
+
spec = Hash[args.map do |name|
|
94
|
+
[name, 1]
|
95
|
+
end]
|
96
|
+
|
97
|
+
return shard_key(spec)
|
98
|
+
end
|
99
|
+
|
100
|
+
if args.length > 2
|
101
|
+
raise ArgumentError, 'Full shard_key syntax requires 1 or 2 arguments'
|
65
102
|
end
|
103
|
+
|
104
|
+
spec, options = args
|
105
|
+
|
106
|
+
spec = Hash[spec.map do |name, value|
|
107
|
+
if value.is_a?(Symbol)
|
108
|
+
value = value.to_s
|
109
|
+
end
|
110
|
+
[database_field_name(name).to_sym, value]
|
111
|
+
end]
|
112
|
+
|
113
|
+
self.shard_key_fields = spec.keys
|
114
|
+
self.shard_config = {
|
115
|
+
key: spec.freeze,
|
116
|
+
options: (options || {}).dup.freeze,
|
117
|
+
}.freeze
|
66
118
|
end
|
67
119
|
end
|
68
120
|
end
|
@@ -5,27 +5,32 @@ namespace :db do
|
|
5
5
|
task :load_models do
|
6
6
|
end
|
7
7
|
|
8
|
-
desc "Create
|
8
|
+
desc "Create indexes specified in Mongoid models"
|
9
9
|
task :create_indexes => [:environment, :load_models] do
|
10
10
|
::Mongoid::Tasks::Database.create_indexes
|
11
11
|
end
|
12
12
|
|
13
|
-
desc "Remove indexes that exist in the database but
|
13
|
+
desc "Remove indexes that exist in the database but are not specified in Mongoid models"
|
14
14
|
task :remove_undefined_indexes => [:environment, :load_models] do
|
15
15
|
::Mongoid::Tasks::Database.remove_undefined_indexes
|
16
16
|
end
|
17
17
|
|
18
|
-
desc "Remove
|
18
|
+
desc "Remove indexes specified in Mongoid models"
|
19
19
|
task :remove_indexes => [:environment, :load_models] do
|
20
20
|
::Mongoid::Tasks::Database.remove_indexes
|
21
21
|
end
|
22
22
|
|
23
|
-
desc "
|
23
|
+
desc "Shard collections with shard keys specified in Mongoid models"
|
24
|
+
task :shard_collections => [:environment, :load_models] do
|
25
|
+
::Mongoid::Tasks::Database.shard_collections
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Drop the database of the default Mongoid client"
|
24
29
|
task :drop => :environment do
|
25
30
|
::Mongoid::Clients.default.database.drop
|
26
31
|
end
|
27
32
|
|
28
|
-
desc "Drop all
|
33
|
+
desc "Drop all non-system collections"
|
29
34
|
task :purge => :environment do
|
30
35
|
::Mongoid.purge!
|
31
36
|
end
|