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

Deprecate locking of dirty records #25873

Merged
merged 1 commit into from
Feb 7, 2017

Conversation

schuetzm
Copy link
Contributor

lock! and with_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

@rails-bot
Copy link

r? @schneems

(@rails-bot has picked a reviewer for you, use r? to override)

@rafaelfranca
Copy link
Member

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?

@schuetzm
Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Member

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

@schuetzm
Copy link
Contributor Author

@rafaelfranca I addressed your comments.

@schuetzm
Copy link
Contributor Author

@rafaelfranca Rebased.

Copy link
Member

@rafaelfranca rafaelfranca left a 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
Copy link
Member

Choose a reason for hiding this comment

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

5.2?

@schuetzm
Copy link
Contributor Author

Added a changelog entry and changed the version in the deprecation notice to 5.2.

@schuetzm schuetzm force-pushed the warn_about_dirty_lock branch 3 times, most recently from 3c76276 to b4532e3 Compare February 7, 2017 10:12
@schuetzm
Copy link
Contributor Author

schuetzm commented Feb 7, 2017

@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?
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@rafaelfranca rafaelfranca merged commit 578f283 into rails:master Feb 7, 2017
rafaelfranca added a commit that referenced this pull request Feb 7, 2017
@schuetzm schuetzm deleted the warn_about_dirty_lock branch February 8, 2017 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants