-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Deprecate locking of dirty records #25873
Conversation
r? @schneems (@rails-bot has picked a reviewer for you, use r? to override) |
Thank you for the pull request. What is the next step after this deprecation? When we remove the deprecation how the code will prevent this to happen again? |
I'd suggest to raise an exception, because that's easy to implement. But thinking about it, there is a way that would just prevent the record from being dirty in the first place: MyModel.where(attr: 42).first_with_lock! do |record|
# change record
record.save!
end On the other hand, this is more complicated, not as handy to use (you can't load the object, pass it to some other method, and lock it there), and might require major refactoring for the end users. So I'd still go with the exception. |
if persisted? | ||
if changed? | ||
ActiveSupport::Deprecation.warn(<<-MSG.squish) | ||
WARNING! Locking a record with unpersisted changes is deprecated. Use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to WARNING! here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Locking a record with unpersisted changes is deprecated and will raise an exception in Rails 5.1. Use
e695f3c
to
57bda0f
Compare
@rafaelfranca I addressed your comments. |
57bda0f
to
7bc818e
Compare
@rafaelfranca Rebased. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a CHANGELOG entry?
if changed? | ||
ActiveSupport::Deprecation.warn(<<-MSG.squish) | ||
Locking a record with unpersisted changes is deprecated and will raise an | ||
exception in Rails 5.1. Use `save` to persist the changes, or `reload` to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5.2?
7bc818e
to
3c76276
Compare
Added a changelog entry and changed the version in the deprecation notice to 5.2. |
3c76276
to
b4532e3
Compare
@rafaelfranca Rebased again. |
@@ -59,7 +59,16 @@ module Pessimistic | |||
# or pass true for "FOR UPDATE" (the default, an exclusive row lock). Returns | |||
# the locked record. | |||
def lock!(lock = true) | |||
reload(lock: lock) if persisted? | |||
if persisted? | |||
if changed? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I forgot to ask before but can you add a test for this deprecation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
b4532e3
to
578f283
Compare
Deprecate locking of dirty records
lock!
andwith_lock
reload the record before doing the actual locking. If there were any unsaved changes, they will be discarded without any warning. IMO this behaviour is dangerous and error prone.See also #19743