Created
January 27, 2022 20:44
-
-
Save boazadato/ba5042717301a566a911a1804c619944 to your computer and use it in GitHub Desktop.
Reproduce a scenario in which mongoid does not update if a different _id is provided
This file contains hidden or 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
require 'mongoid' | |
Mongoid.configure do |config| | |
config.clients.default = { | |
hosts: ['localhost:27017'], | |
database: 'junk_db', | |
} | |
end | |
class Contained | |
include Mongoid::Document | |
embedded_in :container | |
field :contained_fld1, type: String | |
end | |
class Container | |
include Mongoid::Document | |
embeds_one :contained | |
field :container_fld1, type: String | |
end | |
Container.collection.drop | |
contained = Contained.new(contained_fld1: 'contained_fld1_v1') | |
container = Container.new(container_fld1: 'container_fld1_v1') | |
container.contained = contained | |
container.save | |
def assert(cond, message = 'assert failed') | |
unless cond | |
puts Kernel.caller[0...-1].join("\n") | |
raise message | |
end | |
end | |
container = Container.all.first | |
assert(container.container_fld1 == 'container_fld1_v1') | |
assert(container.contained.contained_fld1 == 'contained_fld1_v1') | |
container.update(container_fld1: 'container_fld1_v2', contained: { contained_fld1: 'contained_fld1_v2' }) | |
container = Container.all.first | |
assert(container.container_fld1 == 'container_fld1_v2') | |
assert(container.contained.contained_fld1 == 'contained_fld1_v2') | |
container.update(container_fld1: 'container_fld1_v3', contained: { _id: BSON::ObjectId.new, contained_fld1: 'contained_fld1_v3' }) | |
container = Container.all.first | |
assert(container.container_fld1 == 'container_fld1_v3') | |
# the following ka-boom on mongoid 7.3.* and works on mongoid 7.0.12 | |
# reason is [this change](https://github.com/mongodb/mongoid/commit/802277e3e4beba595e805120be56579724a55c60#diff-4be61127029c58a3f70ac4b78d001ef8f28a597f60c4455e43c6e3938d5cc6f3R74-R75) which is a part of https://jira.mongodb.org/browse/MONGOID-4812 | |
# (which seem an unlrelated change but it actually fix a bug that assumed the bson id would be in :id and not in :_id) | |
# | |
assert(container.contained.contained_fld1 == 'contained_fld1_v3', ':boom:') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment