Skip to content

MONGOID-5199 Reloadable#reload_embedded_document doesn't respect session #5105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion lib/mongoid/reloadable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def reload_root_document
# @return [ Hash ] The reloaded attributes.
def reload_embedded_document
extract_embedded_attributes({}.merge(
collection(_root).find(_root.atomic_selector).read(mode: :primary).first
collection(_root).find(_root.atomic_selector, session: _session).read(mode: :primary).first
))
end

Expand Down
27 changes: 25 additions & 2 deletions spec/mongoid/clients/transactions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@
Mongoid::Clients.with_name(:other).database.collections.each(&:drop)
Mongoid::Clients.with_name(:other).command(create: :people)
Mongoid::Clients.with_name(:other).command(create: :posts)
Mongoid::Clients.with_name(:other).command(create: :canvases)
subscriber.clear_events!
Person.with(client: :other) do
example.run
Canvas.with(client: :other) do
Person.with(client: :other) do
example.run
end
end
Mongoid::Clients.with_name(:other).database.collections.each(&:drop)
end
Expand Down Expand Up @@ -189,6 +192,26 @@
end
end
end

context 'When reloading an embedded document created inside a transaction' do
it 'does not raise an error and has the correct document' do
Canvas.with_session do |s|
s.start_transaction

p = Palette.new
c = Canvas.new(palette: p)
c.save!

expect do
p.reload
end.to_not raise_error

expect(c.palette).to eq(p)

s.commit_transaction
end
end
end
end

context 'when sessions are supported but transactions are not' do
Expand Down