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.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/CHANGELOG.md +6 -6
  5. data/LICENSE +1 -1
  6. data/README.md +5 -5
  7. data/Rakefile +14 -0
  8. data/lib/config/locales/en.yml +5 -5
  9. data/lib/mongoid.rb +3 -2
  10. data/lib/mongoid/association/accessors.rb +37 -2
  11. data/lib/mongoid/association/embedded/embeds_many.rb +2 -1
  12. data/lib/mongoid/association/embedded/embeds_one.rb +2 -1
  13. data/lib/mongoid/association/many.rb +3 -2
  14. data/lib/mongoid/association/proxy.rb +6 -4
  15. data/lib/mongoid/association/referenced/belongs_to/binding.rb +1 -1
  16. data/lib/mongoid/association/referenced/belongs_to/eager.rb +38 -2
  17. data/lib/mongoid/association/referenced/eager.rb +29 -9
  18. data/lib/mongoid/association/referenced/has_many/enumerable.rb +2 -22
  19. data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -2
  20. data/lib/mongoid/atomic.rb +13 -3
  21. data/lib/mongoid/attributes.rb +28 -20
  22. data/lib/mongoid/clients/factory.rb +2 -2
  23. data/lib/mongoid/clients/options.rb +8 -8
  24. data/lib/mongoid/clients/sessions.rb +20 -4
  25. data/lib/mongoid/clients/storage_options.rb +5 -5
  26. data/lib/mongoid/config.rb +42 -12
  27. data/lib/mongoid/config/options.rb +5 -2
  28. data/lib/mongoid/contextual.rb +5 -4
  29. data/lib/mongoid/contextual/geo_near.rb +3 -2
  30. data/lib/mongoid/contextual/map_reduce.rb +3 -2
  31. data/lib/mongoid/contextual/mongo.rb +2 -1
  32. data/lib/mongoid/criteria.rb +23 -4
  33. data/lib/mongoid/criteria/modifiable.rb +2 -1
  34. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -1
  35. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +6 -6
  36. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +12 -0
  37. data/lib/mongoid/criteria/queryable/mergeable.rb +75 -8
  38. data/lib/mongoid/criteria/queryable/pipeline.rb +3 -2
  39. data/lib/mongoid/criteria/queryable/selectable.rb +120 -13
  40. data/lib/mongoid/criteria/queryable/storable.rb +104 -99
  41. data/lib/mongoid/errors/eager_load.rb +2 -0
  42. data/lib/mongoid/errors/no_client_config.rb +2 -2
  43. data/lib/mongoid/errors/no_default_client.rb +1 -1
  44. data/lib/mongoid/extensions/hash.rb +4 -2
  45. data/lib/mongoid/extensions/regexp.rb +1 -1
  46. data/lib/mongoid/fields.rb +2 -1
  47. data/lib/mongoid/fields/standard.rb +2 -1
  48. data/lib/mongoid/fields/validators/macro.rb +4 -1
  49. data/lib/mongoid/findable.rb +5 -4
  50. data/lib/mongoid/interceptable.rb +5 -1
  51. data/lib/mongoid/matchable/regexp.rb +2 -2
  52. data/lib/mongoid/persistable/pushable.rb +11 -2
  53. data/lib/mongoid/persistence_context.rb +6 -6
  54. data/lib/mongoid/query_cache.rb +61 -18
  55. data/lib/mongoid/railties/database.rake +7 -0
  56. data/lib/mongoid/serializable.rb +10 -2
  57. data/lib/mongoid/shardable.rb +56 -4
  58. data/lib/mongoid/tasks/database.rake +10 -5
  59. data/lib/mongoid/tasks/database.rb +83 -0
  60. data/lib/mongoid/timestamps/timeless.rb +3 -1
  61. data/lib/mongoid/validatable/uniqueness.rb +1 -1
  62. data/lib/mongoid/version.rb +1 -1
  63. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +32 -23
  64. data/lib/rails/generators/mongoid/model/templates/model.rb.tt +1 -1
  65. data/spec/app/models/coding.rb +4 -0
  66. data/spec/app/models/coding/pull_request.rb +12 -0
  67. data/spec/app/models/delegating_patient.rb +16 -0
  68. data/spec/app/models/passport.rb +1 -0
  69. data/spec/app/models/phone.rb +1 -0
  70. data/spec/app/models/publication.rb +5 -0
  71. data/spec/app/models/publication/encyclopedia.rb +12 -0
  72. data/spec/app/models/publication/review.rb +14 -0
  73. data/spec/integration/app_spec.rb +254 -0
  74. data/spec/integration/associations/embedded_spec.rb +54 -0
  75. data/spec/integration/associations/has_many_spec.rb +34 -0
  76. data/spec/integration/associations/has_one_spec.rb +34 -0
  77. data/spec/integration/bson_regexp_raw_spec.rb +20 -0
  78. data/spec/integration/criteria/date_field_spec.rb +41 -0
  79. data/spec/integration/criteria/logical_spec.rb +13 -0
  80. data/spec/integration/document_spec.rb +22 -0
  81. data/spec/integration/shardable_spec.rb +149 -0
  82. data/spec/lite_spec_helper.rb +15 -4
  83. data/spec/mongoid/association/accessors_spec.rb +238 -63
  84. data/spec/mongoid/association/embedded/embeds_many_models.rb +19 -0
  85. data/spec/mongoid/association/embedded/embeds_many_spec.rb +10 -0
  86. data/spec/mongoid/association/embedded/embeds_one_spec.rb +0 -2
  87. data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +193 -10
  88. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +140 -1
  89. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +146 -68
  90. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +2 -1
  91. data/spec/mongoid/attributes_spec.rb +19 -7
  92. data/spec/mongoid/changeable_spec.rb +23 -0
  93. data/spec/mongoid/clients/factory_spec.rb +8 -8
  94. data/spec/mongoid/clients/options_spec.rb +11 -11
  95. data/spec/mongoid/clients/sessions_spec.rb +8 -4
  96. data/spec/mongoid/clients/transactions_spec.rb +20 -8
  97. data/spec/mongoid/clients_spec.rb +2 -2
  98. data/spec/mongoid/contextual/atomic_spec.rb +22 -11
  99. data/spec/mongoid/contextual/geo_near_spec.rb +11 -2
  100. data/spec/mongoid/contextual/map_reduce_spec.rb +20 -5
  101. data/spec/mongoid/contextual/mongo_spec.rb +76 -53
  102. data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +1 -1
  103. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +7 -7
  104. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +1 -1
  105. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +19 -7
  106. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +28 -1
  107. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +45 -12
  108. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +1051 -392
  109. data/spec/mongoid/criteria/queryable/selectable_spec.rb +52 -0
  110. data/spec/mongoid/criteria/queryable/storable_spec.rb +80 -2
  111. data/spec/mongoid/criteria_spec.rb +36 -2
  112. data/spec/mongoid/document_fields_spec.rb +29 -0
  113. data/spec/mongoid/document_persistence_context_spec.rb +33 -0
  114. data/spec/mongoid/errors/no_client_config_spec.rb +2 -2
  115. data/spec/mongoid/errors/no_client_database_spec.rb +3 -3
  116. data/spec/mongoid/errors/no_client_hosts_spec.rb +3 -3
  117. data/spec/mongoid/fields_spec.rb +24 -1
  118. data/spec/mongoid/indexable_spec.rb +6 -4
  119. data/spec/mongoid/interceptable_spec.rb +62 -0
  120. data/spec/mongoid/interceptable_spec_models.rb +76 -0
  121. data/spec/mongoid/matchable/default_spec.rb +1 -1
  122. data/spec/mongoid/matchable/regexp_spec.rb +2 -2
  123. data/spec/mongoid/matchable_spec.rb +2 -2
  124. data/spec/mongoid/persistable/pushable_spec.rb +55 -1
  125. data/spec/mongoid/query_cache_spec.rb +77 -9
  126. data/spec/mongoid/relations/proxy_spec.rb +1 -1
  127. data/spec/mongoid/scopable_spec.rb +2 -1
  128. data/spec/mongoid/serializable_spec.rb +129 -18
  129. data/spec/mongoid/shardable_models.rb +61 -0
  130. data/spec/mongoid/shardable_spec.rb +69 -16
  131. data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
  132. data/spec/mongoid/tasks/database_spec.rb +1 -1
  133. data/spec/spec_helper.rb +2 -31
  134. data/spec/support/child_process_helper.rb +76 -0
  135. data/spec/support/cluster_config.rb +3 -3
  136. data/spec/support/constraints.rb +26 -10
  137. data/spec/support/expectations.rb +3 -1
  138. data/spec/support/helpers.rb +11 -0
  139. data/spec/support/lite_constraints.rb +22 -0
  140. data/spec/support/session_registry.rb +50 -0
  141. data/spec/support/spec_config.rb +12 -4
  142. metadata +518 -480
  143. metadata.gz.sig +0 -0
@@ -6,6 +6,8 @@ module Mongoid
6
6
 
7
7
  # This error is raised when attempting to eager load a many to many
8
8
  # association.
9
+ #
10
+ # @deprecated No longer used by Mongoid per MONGOID-4841.
9
11
  class EagerLoad < MongoidError
10
12
 
11
13
  # Create the new eager load error.
@@ -11,9 +11,9 @@ module Mongoid
11
11
  # Create the new error.
12
12
  #
13
13
  # @example Create the error.
14
- # NoClientConfig.new(:secondary)
14
+ # NoClientConfig.new(:analytics)
15
15
  #
16
- # @param [ String, Symbol ] name The name of the client.
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([ :secondary ])
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
- (consolidated[key] ||= {}).merge!(value)
51
+ consolidated[key] ||= {}
52
+ consolidated[key].update(value)
52
53
  else
53
- (consolidated["$set"] ||= {}).merge!(key => mongoize_for(key, klass, key, value))
54
+ consolidated["$set"] ||= {}
55
+ consolidated["$set"].update(key => mongoize_for(key, klass, key, value))
54
56
  end
55
57
  end
56
58
  consolidated
@@ -11,7 +11,7 @@ module Mongoid
11
11
  # type.
12
12
  #
13
13
  # @example Mongoize the object.
14
- # Regexp.mongoize(/^[abc]/)
14
+ # Regexp.mongoize(/\A[abc]/)
15
15
  #
16
16
  # @param [ Regexp, String ] object The object to mongoize.
17
17
  #
@@ -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
- (attributes[name] ||= {}).with_indifferent_access
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
- delegate :demongoize, :evolve, :mongoize, to: :type
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
- Mongoid.logger.warn(FIELD_TYPE_IS_SYMBOL)
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
@@ -8,15 +8,16 @@ module Mongoid
8
8
  #
9
9
  # @since 4.0.0
10
10
  module Findable
11
+ extend Forwardable
11
12
 
12
- delegate *(
13
+ def_delegators :with_default_scope, *(
13
14
  Criteria::Queryable::Selectable.forwardables +
14
15
  Criteria::Queryable::Optional.forwardables
15
- ), to: :with_default_scope
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
- delegate \
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, to: :with_default_scope
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.
@@ -131,7 +131,11 @@ module Mongoid
131
131
  return false
132
132
  end
133
133
  end
134
- callback_executable?(kind) ? super(kind, *args, &block) : true
134
+ if callback_executable?(kind)
135
+ super(kind, *args, &block)
136
+ else
137
+ true
138
+ end
135
139
  end
136
140
 
137
141
  private
@@ -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?(/^Em/)
14
- # matcher._matches?(BSON::Regex::Raw.new("^Em"))
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) || (attributes[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) || (attributes[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)
@@ -163,13 +163,13 @@ module Mongoid
163
163
  private
164
164
 
165
165
  def process(result)
166
- @remaining -= result.returned_count if limited?
167
- @cursor_id = result.cursor_id
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
- (@cached_documents ||= []).concat(documents)
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
- unless cursor = cached_cursor
230
- read_with_retry do
231
- server = server_selector.select_server(cluster)
232
- cursor = CachedCursor.new(view, send_initial_query(server), server)
233
- QueryCache.cache_table[cache_key] = cursor
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
- cursor.each do |doc|
237
- yield doc
238
- end if block_given?
239
- cursor
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)
@@ -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
- undef_method :include_root_in_json
17
- delegate :include_root_in_json, to: ::Mongoid
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
@@ -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 :first_name, :last_name
82
+ # shard_key first_name: 1, last_name: 1
59
83
  # end
60
84
  #
61
85
  # @since 2.0.0
62
- def shard_key(*names)
63
- names.each do |name|
64
- self.shard_key_fields << self.database_field_name(name).to_sym
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 the indexes defined on your mongoid models"
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 aren't specified on the models"
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 the indexes defined on your mongoid models without questions!"
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 "Drops the default client database"
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 collections except the system collections"
33
+ desc "Drop all non-system collections"
29
34
  task :purge => :environment do
30
35
  ::Mongoid.purge!
31
36
  end