Skip to content

Commit

Permalink
Merge pull request #4496 from estolfo/MONGOID-4536-session
Browse files Browse the repository at this point in the history
MONGOID-4536 Change session accessor name on a model to avoid name clash
  • Loading branch information
estolfo authored May 2, 2018
2 parents 49f9b82 + 05dc98a commit eb3e597
Show file tree
Hide file tree
Showing 17 changed files with 36 additions and 36 deletions.
4 changes: 2 additions & 2 deletions lib/mongoid/clients/sessions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def with_session(options = {})

private

def session
def _session
Threaded.get_session
end

Expand Down Expand Up @@ -104,7 +104,7 @@ def with_session(options = {})

private

def session
def _session
Threaded.get_session
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/contextual/aggregable/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Mongo
#
# @since 3.0.0
def aggregates(field)
result = collection.find.aggregate(pipeline(field), session: session).to_a
result = collection.find.aggregate(pipeline(field), session: _session).to_a
if result.empty?
{ "count" => 0, "sum" => nil, "avg" => nil, "min" => nil, "max" => nil }
else
Expand Down
6 changes: 3 additions & 3 deletions lib/mongoid/contextual/map_reduce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def raw
validate_out!
cmd = command
opts = { read: cmd.delete(:read).options } if cmd[:read]
@map_reduce.database.command(cmd, (opts || {}).merge(session: session)).first
@map_reduce.database.command(cmd, (opts || {}).merge(session: _session)).first
end
alias :results :raw

Expand Down Expand Up @@ -250,8 +250,8 @@ def validate_out!
raise Errors::NoMapReduceOutput.new({}) unless @map_reduce.out
end

def session
criteria.send(:session)
def _session
criteria.send(:_session)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/mongoid/contextual/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def delete
unless removed.empty?
collection.find(selector).update_one(
positionally(selector, "$pullAll" => { path => removed }),
session: session
session: _session
)
end
deleted
Expand Down Expand Up @@ -304,7 +304,7 @@ def update_documents(attributes, docs)
updates["$set"].merge!(doc.atomic_updates["$set"] || {})
doc.move_changes
end
collection.find(selector).update_one(updates, session: session) unless updates["$set"].empty?
collection.find(selector).update_one(updates, session: _session) unless updates["$set"].empty?
end

# Get the limiting value.
Expand Down Expand Up @@ -446,8 +446,8 @@ def prepare_remove(doc)
doc.destroyed = true
end

def session
@criteria.send(:session)
def _session
@criteria.send(:_session)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/mongoid/contextual/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def initialize(criteria)
@criteria, @klass, @cache = criteria, criteria.klass, criteria.options[:cache]
@collection = @klass.collection
criteria.send(:merge_type_selection)
@view = collection.find(criteria.selector, session: session)
@view = collection.find(criteria.selector, session: _session)
apply_options
end

Expand Down Expand Up @@ -706,8 +706,8 @@ def yield_document(document, &block)
documents.push(doc) if cacheable?
end

def session
@criteria.send(:session)
def _session
@criteria.send(:_session)
end

def acknowledged_write?
Expand Down
8 changes: 4 additions & 4 deletions lib/mongoid/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ def create_indexes
key, options = spec.key, spec.options
if database = options[:database]
with(database: database) do |klass|
klass.collection.indexes(session: session).create_one(key, options.except(:database))
klass.collection.indexes(session: _session).create_one(key, options.except(:database))
end
else
collection.indexes(session: session).create_one(key, options)
collection.indexes(session: _session).create_one(key, options)
end
end and true
end
Expand All @@ -53,9 +53,9 @@ def remove_indexes
indexed_database_names.each do |database|
with(database: database) do |klass|
begin
klass.collection.indexes(session: session).each do |spec|
klass.collection.indexes(session: _session).each do |spec|
unless spec["name"] == "_id_"
klass.collection.indexes(session: session).drop_one(spec["key"])
klass.collection.indexes(session: _session).drop_one(spec["key"])
logger.info(
"MONGOID: Removed index '#{spec["name"]}' on collection " +
"'#{klass.collection.name}' in database '#{database}'."
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/persistable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def persist_or_delay_atomic_operation(operation)
def persist_atomic_operations(operations)
if persisted? && operations
selector = atomic_selector
_root.collection.find(selector).update_one(positionally(selector, operations), session: session)
_root.collection.find(selector).update_one(positionally(selector, operations), session: _session)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/persistable/creatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def insert_as_embedded
selector = _parent.atomic_selector
_root.collection.find(selector).update_one(
positionally(selector, atomic_inserts),
session: session)
session: _session)
end
end

Expand All @@ -78,7 +78,7 @@ def insert_as_embedded
#
# @since 4.0.0
def insert_as_root
collection.insert_one(as_attributes, session: session)
collection.insert_one(as_attributes, session: _session)
end

# Post process an insert, which sets the new record attribute to false
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/persistable/deletable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def delete_as_embedded(options = {})
selector = _parent.atomic_selector
_root.collection.find(selector).update_one(
positionally(selector, atomic_deletes),
session: session)
session: _session)
end
true
end
Expand All @@ -80,7 +80,7 @@ def delete_as_embedded(options = {})
#
# @since 4.0.0
def delete_as_root
collection.find(atomic_selector).delete_one(session: session)
collection.find(atomic_selector).delete_one(session: _session)
true
end

Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/persistable/updatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ def update_document(options = {})
unless updates.empty?
coll = collection(_root)
selector = atomic_selector
coll.find(selector).update_one(positionally(selector, updates), session: session)
coll.find(selector).update_one(positionally(selector, updates), session: _session)
conflicts.each_pair do |key, value|
coll.find(selector).update_one(positionally(selector, { key => value }), session: session)
coll.find(selector).update_one(positionally(selector, { key => value }), session: _session)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/persistable/upsertable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Upsertable
def upsert(options = {})
prepare_upsert(options) do
collection.find(atomic_selector).update_one(
as_attributes, upsert: true, session: session)
as_attributes, upsert: true, session: _session)
end
end

Expand Down
8 changes: 4 additions & 4 deletions lib/mongoid/relations/embedded/batchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def batch_clear(docs)
unless docs.empty?
collection.find(selector).update_one(
positionally(selector, "$unset" => { path => true }),
session: session
session: _session
)
post_process_batch_remove(docs, :delete)
end
Expand All @@ -60,7 +60,7 @@ def batch_remove(docs, method = :delete)
if !docs.empty?
collection.find(selector).update_one(
positionally(selector, "$pullAll" => { path => removals }),
session: session
session: _session
)
post_process_batch_remove(docs, method)
end
Expand Down Expand Up @@ -136,7 +136,7 @@ def execute_batch_set(docs)
if insertable?
collection.find(selector).update_one(
positionally(selector, '$set' => { path => inserts }),
session: session
session: _session
)
post_process_batch_insert(docs)
end
Expand All @@ -161,7 +161,7 @@ def execute_batch_push(docs)
if insertable?
collection.find(selector).update_one(
positionally(selector, '$push' => { path => { '$each' => pushes } }),
session: session
session: _session
)
post_process_batch_insert(docs)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/relations/many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ def unscoped

private

def session
base.send(:session)
def _session
base.send(:_session)
end

# Find the first object given the supplied attributes or create/initialize it.
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/relations/referenced/many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ def method_missing(name, *args, &block)
# @since 3.0.0
def persist_delayed(docs, inserts)
unless docs.empty?
collection.insert_many(inserts, session: session)
collection.insert_many(inserts, session: _session)
docs.each do |doc|
doc.new_record = false
doc.run_after_callbacks(:create, :save)
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/relations/touchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def touch(field = nil)
touches = touch_atomic_updates(field)
unless touches["$set"].blank?
selector = atomic_selector
_root.collection.find(selector).update_one(positionally(selector, touches), session: session)
_root.collection.find(selector).update_one(positionally(selector, touches), session: _session)
end
run_callbacks(:touch)
true
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/reloadable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def _reload
#
# @since 2.3.2
def reload_root_document
{}.merge(collection.find({ _id: _id }, session: session).read(mode: :primary).first || {})
{}.merge(collection.find({ _id: _id }, session: _session).read(mode: :primary).first || {})
end

# Reload the embedded document.
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/tasks/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def undefined_indexes(models = ::Mongoid.models)
models.each do |model|
unless model.embedded?
begin
model.collection.indexes(session: model.send(:session)).each do |index|
model.collection.indexes(session: model.send(:_session)).each do |index|
# ignore default index
unless index['name'] == '_id_'
key = index['key'].symbolize_keys
Expand Down Expand Up @@ -77,7 +77,7 @@ def remove_undefined_indexes(models = ::Mongoid.models)
indexes.each do |index|
key = index['key'].symbolize_keys
collection = model.collection
collection.indexes(session: model.send(:session)).drop_one(key)
collection.indexes(session: model.send(:_session)).drop_one(key)
logger.info(
"MONGOID: Removed index '#{index['name']}' on collection " +
"'#{collection.name}' in database '#{collection.database.name}'."
Expand Down

0 comments on commit eb3e597

Please sign in to comment.