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

Projects
None yet
6 participants
@schuetzm
Contributor

schuetzm commented Jul 18, 2016

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

This comment has been minimized.

rails-bot commented Jul 18, 2016

r? @schneems

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

@rafaelfranca

This comment has been minimized.

Member

rafaelfranca commented Jul 20, 2016

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

This comment has been minimized.

Contributor

schuetzm commented Jul 20, 2016

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.

@rafaelfranca

View changes

activerecord/lib/active_record/locking/pessimistic.rb Outdated
if persisted?
if changed?
ActiveSupport::Deprecation.warn(<<-MSG.squish)
WARNING! Locking a record with unpersisted changes is deprecated. Use

This comment has been minimized.

@rafaelfranca

rafaelfranca Jul 20, 2016

Member

No need to WARNING! here.

This comment has been minimized.

@rafaelfranca

rafaelfranca Jul 20, 2016

Member

Locking a record with unpersisted changes is deprecated and will raise an exception in Rails 5.1. Use

@rafaelfranca

View changes

activerecord/lib/active_record/locking/pessimistic.rb Outdated
`save` to persist the changes, or `reload` to discard them explicitly.
MSG
end
reload(:lock => lock)

This comment has been minimized.

@rafaelfranca

rafaelfranca Jul 20, 2016

Member

Use Ruby 1.9 hash syntax here.

@schuetzm schuetzm force-pushed the schuetzm:warn_about_dirty_lock branch Jul 21, 2016

@schuetzm

This comment has been minimized.

Contributor

schuetzm commented Jul 21, 2016

@rafaelfranca I addressed your comments.

@schuetzm schuetzm force-pushed the schuetzm:warn_about_dirty_lock branch Jan 18, 2017

@schuetzm

This comment has been minimized.

Contributor

schuetzm commented Jan 18, 2017

@rafaelfranca Rebased.

@rafaelfranca

Could you add a CHANGELOG entry?

activerecord/lib/active_record/locking/pessimistic.rb Outdated
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

This comment has been minimized.

@kamipo

@schuetzm schuetzm force-pushed the schuetzm:warn_about_dirty_lock branch Jan 19, 2017

@schuetzm

This comment has been minimized.

Contributor

schuetzm commented Jan 19, 2017

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

@schuetzm schuetzm force-pushed the schuetzm:warn_about_dirty_lock branch 3 times, most recently Feb 7, 2017

@schuetzm

This comment has been minimized.

Contributor

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?

This comment has been minimized.

@rafaelfranca

rafaelfranca Feb 7, 2017

Member

Sorry I forgot to ask before but can you add a test for this deprecation?

This comment has been minimized.

@schuetzm

schuetzm Feb 7, 2017

Contributor

Done.

@schuetzm schuetzm force-pushed the schuetzm:warn_about_dirty_lock branch to 578f283 Feb 7, 2017

@rafaelfranca rafaelfranca merged commit 578f283 into rails:master Feb 7, 2017

2 checks passed

codeclimate no new or fixed issues
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

rafaelfranca added a commit that referenced this pull request Feb 7, 2017

Merge pull request #25873 from schuetzm/warn_about_dirty_lock
Deprecate locking of dirty records

@schuetzm schuetzm deleted the schuetzm:warn_about_dirty_lock branch Feb 8, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment