aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2017-02-07 14:39:51 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2017-02-07 14:39:51 -0300
commit90fb779873971564e28b03a738749904d5c07f07 (patch)
tree72a8eb8b541ba3524cac9d19655b003439f4bdd1
parent9e98f3f7e61dfce0a48948c8d296400af8bfaf21 (diff)
parent578f283012f2f047b9e79ac046a32fd51e274761 (diff)
downloadrails-90fb779873971564e28b03a738749904d5c07f07.tar.gz
rails-90fb779873971564e28b03a738749904d5c07f07.tar.bz2
rails-90fb779873971564e28b03a738749904d5c07f07.zip
Merge pull request #25873 from schuetzm/warn_about_dirty_lock
Deprecate locking of dirty records
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/locking/pessimistic.rb11
-rw-r--r--activerecord/test/cases/locking_test.rb5
3 files changed, 18 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 7546b7edac..bae2cab457 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -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*
diff --git a/activerecord/lib/active_record/locking/pessimistic.rb b/activerecord/lib/active_record/locking/pessimistic.rb
index e73cb4fc12..263e2a5f7f 100644
--- a/activerecord/lib/active_record/locking/pessimistic.rb
+++ b/activerecord/lib/active_record/locking/pessimistic.rb
@@ -59,7 +59,16 @@ module ActiveRecord
# 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
diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb
index 9e42242e55..23095618a4 100644
--- a/activerecord/test/cases/locking_test.rb
+++ b/activerecord/test/cases/locking_test.rb
@@ -536,7 +536,10 @@ unless in_memory_db?
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