-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MONGOID-5370 Add collection options support (#5452)
Co-authored-by: Oleg Pudeyev <[email protected]>
- Loading branch information
1 parent
0e5a20e
commit 7f6fd08
Showing
15 changed files
with
618 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mongoid | ||
|
||
# Encapsulates behavior around defining collections. | ||
module CollectionConfigurable | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
# Create the collection for the called upon Mongoid model. | ||
# | ||
# This method does not re-create existing collections. | ||
# | ||
# If the document includes `store_in` macro with `collection_options` key, | ||
# these options are used when creating the collection. | ||
# | ||
# @param [ true | false ] force If true, the method will drop existing | ||
# collections before creating new ones. If false, the method will create | ||
# only new collection (that do not exist in the database). | ||
# | ||
# @raise [ Errors::CreateCollectionFailure ] If collection creation failed. | ||
# @raise [ Errors::DropCollectionFailure ] If an attempt to drop collection failed. | ||
def create_collection(force: false) | ||
if collection_name.empty? | ||
# This is most probably an anonymous class, we ignore them. | ||
return | ||
end | ||
if collection_name.match(/^system\./) | ||
# We do not do anything with system collections. | ||
return | ||
end | ||
if force | ||
collection.drop | ||
end | ||
if coll_options = collection.database.list_collections(filter: { name: collection_name.to_s }).first | ||
if force | ||
raise Errors::DropCollectionFailure.new(collection_name) | ||
else | ||
logger.debug( | ||
"MONGOID: Collection '#{collection_name}' already exists " + | ||
"in database '#{database_name}' with options '#{coll_options}'." | ||
) | ||
end | ||
else | ||
begin | ||
collection.database[collection_name, storage_options.fetch(:collection_options, {})].create | ||
rescue Mongo::Error::OperationFailure => e | ||
raise Errors::CreateCollectionFailure.new( | ||
collection_name, | ||
storage_options[:collection_options], | ||
e | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mongoid | ||
module Errors | ||
|
||
# Raised when an attempt to create a collection failed. | ||
class CreateCollectionFailure < MongoidError | ||
|
||
# Instantiate the create collection error. | ||
# | ||
# @param [ String ] collection_name The name of the collection that | ||
# Mongoid failed to create. | ||
# @param [ Hash ] collection_options The options that were used when | ||
# tried to create the collection. | ||
# @param [ Mongo::Error::OperationFailure ] error The error raised when | ||
# tried to create the collection. | ||
# | ||
# @api private | ||
def initialize(collection_name, collection_options, error) | ||
super( | ||
compose_message( | ||
"create_collection_failure", | ||
{ | ||
collection_name: collection_name, | ||
collection_options: collection_options, | ||
error: "#{error.class}: #{error.message}" | ||
} | ||
) | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mongoid | ||
module Errors | ||
|
||
# Raised when an attempt to drop a collection failed. | ||
class DropCollectionFailure < MongoidError | ||
|
||
# Instantiate the drop collection error. | ||
# | ||
# @param [ String ] collection_name The name of the collection that | ||
# Mongoid failed to drop. | ||
# | ||
# @api private | ||
def initialize(collection_name, collection_options, error) | ||
super( | ||
compose_message( | ||
"drop_collection_failure", | ||
{ | ||
collection_name: collection_name | ||
} | ||
) | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.