MONGOID-5078 Fix shard_key_selector_in_db during post-persist callbacks#4991
Conversation
|
I am converting this PR to "non-draft mode" because it won't assign reviewers otherwise. |
|
@dalton-braze |
@johnnyshields, would you be able to clarify your suggestion? How I'm parsing it, I'm not seeing how changing the point-in-time that The key section of this PR is the following updated method: def shard_key_selector_in_db
selector = {}
shard_key_fields.each do |field|
selector[field.to_s] = new_record? || during_post_persist_callbacks ? send(field) : attribute_was(field)
end
selector
endIf my explanation feels ambiguous, I'd suggest checking out the repro test cases I've written in As for how |
|
@dalton-braze sorry my comment was mistaken, please ignore. |
|
@comandeo this might be a good one for you to look at given your expertise with |
|
@johnnyshields @dalton-braze The issue seems to be fixed here - 9fa2cc3 I think we can close this PR. |
|
@comandeo can we merge in just the tests from this PR? (or tests that demonstrate the fix, in other words) |
|
@comandeo mind taking a look here again and deciding if we could close this one? |
|
@neilshweky the problem here is certainly a legitimate one for users who use sharding. Ticket should be kept open. |
Hi @p and @comandeo,
The mongoid#4985 PR allowed me to tweak my existing solution draft for MONGOID-5078 because of
shard_key_selector_in_dbbeing split out ofshard_key_selector.shard_key_selector_in_dbis defined as:This supports the conclusion that
shard_key_selector_in_dbshould contain the updated version of a shard key during "post-persist" callbacks (after_create,after_save,after_update,after_upsert, etc). The current incorrect behavior is thatshard_key_selector_in_dbreturns the old value of a shard key during "post-persist" callbacks which are triggered by updating a shard key.The potential solution I designed adds a single instance variable,
@during_post_persist_callbacks, to eachMongoid::Document. This instance variable is read byshard_key_selector_in_dbto determine if it should return a sharded field's internally cached previous value or its current value.This PR is intended as a draft (especially the example tests I wrote). I am hoping to use it as a jumping-off point for discussing the solution to this behavior in more detail. I'd love to hear what you think.
For a much deeper level of detail, check out my original open PR here.