-
Notifications
You must be signed in to change notification settings - Fork 902
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
enforce_version_limit! assumes database records are returned in correct order #940
Comments
Thanks, Dan. This all sounds good.
I'll look for your PR, thanks. Heroic effort on the bug report, btw. ⭐️ Not an easy thing to simulate. |
Thanks! I'll follow up with you on the PR. |
jaredbeck
pushed a commit
that referenced
this issue
Mar 28, 2017
aried3r
pushed a commit
to aried3r/paper_trail
that referenced
this issue
Dec 14, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When setting
PaperTrail.config.version_limit
to N, the expectation is that, when an update creates the (N+1)th version of a record, the oldest versions for that record will be deleted.This behavior is implemented in
PaperTrail::VersionConcern#enforce_version_limit!
, which doesn't order the versions bycreated_at
, so it deletes the last versions based on the order the database returns them. This is very often insertion order, but it's often some other order, based on whatever is efficient for the database. You can verify this by callingto_sql
on the relation that's used, eg,sibling_versions.to_sql
. You get this (reformatted with whitespace):This behavior - the database returning records in a different order - is hard to reproduce reliably, but is a generally-understood property of RDBMs, so I felt justified in hacking to simulate it. While investigating this issue and working on a fix, I wrote a spec to verify that
#enforce_version_limit!
deletes the wrong records when they're returned out-of-insertion-order. When I noticed your bug-report template requires a reproduction, I reused that spec (only a minor port from rspec to minitest).I'll submit a PR with the spec, and the change that fixes it (by adding an
ORDER
clause), shortly. The new SQL will be:The text was updated successfully, but these errors were encountered: