Fix deleted time order between associations#154
Merged
wata727 merged 1 commit intokufu:masterfrom Jan 31, 2024
Merged
Conversation
wata727
commented
Jan 30, 2024
| end | ||
|
|
||
| it "create state-destroy record before _run_destroy_callbacks" do | ||
| it "create state-destroy record around _run_destroy_callbacks" do |
Contributor
Author
There was a problem hiding this comment.
This description is already does not represent the actual scenario, so I have revised it.
Perhaps the description no longer matches what has already been tested in #19.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a strange problem with deletion times when deleting a model with a Bi-Temporal model association with
dependent: :destroy.Normally, when an association is deleted by
dependent: :destroy, it is deleted before its parent bybefore_destroycallback.https://github.com/rails/rails/blob/v7.1.3/activerecord/lib/active_record/associations/builder/association.rb#L141
However, strangely, the bitemporal gem creates a history where the parent is deleted first:
Why does this happen? The cause is the timing of calculating the operation time.
ActiveRecord::Bitemporal::Persistence#destroyis assigned the time when it is called asoperated_at:activerecord-bitemporal/lib/activerecord-bitemporal/bitemporal.rb
Line 347 in 21420ef
On the other hand, the callback fires inside the
destroymethod:activerecord-bitemporal/lib/activerecord-bitemporal/bitemporal.rb
Line 356 in 21420ef
This causes the actual deletion to occur from child to parent, but the operation time is calculated from parent to child.
This PR fixes the problem by calculating the operation time inside the destroy callback.