Skip to content
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

Apply irrelevant changes caused by touch #140

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

wata727
Copy link
Contributor

@wata727 wata727 commented Jul 19, 2023

While doing some research, I noticed that models that include ActiveRecord::Bitemporal have unintended changes after being touched:

# bitemporal
employee.changes.keys # => []
employee.touch
employee.changes.keys # => ["valid_from", "transaction_from"]
employee.saved_changes.keys # => ["updated_at"]

# non-bitemporal
book.changes.keys # => []
book.touch
book,changes.keys # => []
book.saved_changes.keys # => ["updated_at"]

Perhaps adding changes unintentionally is not the expected behavior.

This PR changes the changes caused by touch to be moved saved_changes:

employee.changes.keys # => []
employee.touch
employee.changes.keys # => []
employee.saved_changes.keys # => ["valid_from", "transaction_from", "updated_at"]

As a side note, I noticed that unlike the non-bitemporal model, there was an issue where the dirty state before touch was unintentionally saved, but that's an issue that should be addressed in another PR.

@@ -329,6 +329,10 @@ def _update_row(attribute_names, attempted_action = 'update')
self.transaction_from = after_instance.transaction_from
self.transaction_to = after_instance.transaction_to

if attempted_action == 'touch'
@_touch_attr_names += Set.new(['valid_from', 'valid_to', 'transaction_from', 'transaction_to'])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Append to an instance variable used by ActiveRecord::AttributeMethods::Dirty#_touch_row.
https://github.com/rails/rails/blob/v7.0.6/activerecord/lib/active_record/attribute_methods/dirty.rb#L187

This is probably not a good approach, but it's the only way to write out to the saved_changes at once with minimal changes.

@@ -27,10 +27,13 @@
it {
is_expected.to have_attributes(
bitemporal_id: subject.id,
# changes: be_empty,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the expected is be_empty, but unfortunately that is not the case at this time. This will be fixed in another PR.

@@ -1052,6 +1072,8 @@ class EmployeeWithUniquness < Employee
it { expect { subject }.to change { Employee.ignore_valid_datetime.within_deleted.count }.by(1) }
it { expect { subject }.to change(employee, :swapped_id).from(@swapped_id_before_destroy).to(kind_of(Integer)) }
it { expect { subject }.to change(employee, :swapped_id_previously_was).from(kind_of(Integer)).to(@swapped_id_before_destroy) }
xit { expect { subject }.to change(employee, :changes).to(be_empty) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the expected is be_empty, but unfortunately that is not the case at this time. This will be fixed in another PR.

@wata727 wata727 marked this pull request as ready for review July 20, 2023 10:38
@auto-assign auto-assign bot requested review from lighty and yono July 20, 2023 10:38
@wata727 wata727 requested review from osyo-manga, krororo and Dooor and removed request for yono and lighty July 20, 2023 10:38
Copy link
Collaborator

@krororo krororo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@Dooor Dooor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wata727
Copy link
Contributor Author

wata727 commented Oct 10, 2023

This PR is frozen because #148 may fix this issue.

@wata727 wata727 marked this pull request as draft October 10, 2023 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants