Skip to content

Commit

Permalink
Deprecate locking of dirty records
Browse files Browse the repository at this point in the history
  • Loading branch information
schuetzm committed Feb 7, 2017
1 parent adaa358 commit 578f283
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*

* Deprecate `ColumnDumper#migration_keys`.

*Ryuta Kamizono*
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 578f283

Please sign in to comment.