Skip to content

Commit

Permalink
Merge pull request #25873 from schuetzm/warn_about_dirty_lock
Browse files Browse the repository at this point in the history
Deprecate locking of dirty records
  • Loading branch information
rafaelfranca committed Feb 7, 2017
2 parents 9e98f3f + 578f283 commit 90fb779
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Deprecate locking records with unpersisted changes.

*Marc Schütz*

* Remove deprecated behavior that halts callbacks when the return is false.

*Rafael Mendonça França*
Expand Down
11 changes: 10 additions & 1 deletion activerecord/lib/active_record/locking/pessimistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Locking a record with unpersisted changes is deprecated and will raise an
exception in Rails 5.2. Use `save` to persist the changes, or `reload` to
discard them explicitly.
MSG
end
reload(lock: lock)
end
self
end

Expand Down
5 changes: 4 additions & 1 deletion activerecord/test/cases/locking_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,10 @@ def test_sane_lock_method
Person.transaction do
person = Person.find 1
old, person.first_name = person.first_name, "fooman"
person.lock!
# Locking a dirty record is deprecated
assert_deprecated do
person.lock!
end
assert_equal old, person.first_name
end
end
Expand Down

0 comments on commit 90fb779

Please sign in to comment.