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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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?
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.

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