Skip to content
This repository has been archived by the owner on Jan 31, 2019. It is now read-only.

Change write concern (safety) options to work with MongoDB 2.6+ #9

Open
wants to merge 1 commit into
base: 2.5.0-stable-backports
Choose a base branch
from
Open
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
27 changes: 25 additions & 2 deletions lib/mongoid/safety.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,37 @@ class << self
# @since 2.1.0
def merge_safety_options(options = {})
options ||= {}
return options if options[:safe]
return convert_safety_options_to_mongo_2_6(options) if options[:safe]

unless Threaded.safety_options.nil?
safety = Threaded.safety_options
else
safety = Mongoid.persist_in_safe_mode
end
options.merge!({ :safe => safety })

convert_safety_options_to_mongo_2_6(options.merge({ :safe => safety }))
end
def convert_safety_options_to_mongo_2_6(options)
writeConcern = {}

# Instead of intermingling write-concern options into the main options
# hash, starting with MongoDB 2.6, they should be specified in a :writeConcern
# option.

# Old style { :safe => true }
if (options.include?(:safe))
writeConcern[:w] = options.delete(:safe) ? 1 : 0
end

# Newer style { :w => 1, :j => 1 }
writeConcern[:w] = options.delete(:w) if options.include?(:w)
writeConcern[:j] = options.delete(:j) if options.include?(:j)

# Unsupported legacy options: :wtimeout and :fsync
options.delete(:wtimeout)
options.delete(:fsync)

options.merge!( :writeConcern => writeConcern )
end
end

Expand Down