aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-03-17 15:48:47 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-03-17 15:48:47 +0000
commita38f28fff1e9e3faeeb22ac9b7c6b3cdcb7e2b29 (patch)
treee0f0d882ca5bfd56704dad93f56235c43266f5c7 /activerecord
parentf87db851c680164b6474d783d5f29b6cb4c013c0 (diff)
downloadrails-a38f28fff1e9e3faeeb22ac9b7c6b3cdcb7e2b29.tar.gz
rails-a38f28fff1e9e3faeeb22ac9b7c6b3cdcb7e2b29.tar.bz2
rails-a38f28fff1e9e3faeeb22ac9b7c6b3cdcb7e2b29.zip
Base.update_all :order and :limit options. Useful for MySQL updates that must be ordered to avoid violating unique constraints.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6440 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/base.rb10
-rwxr-xr-xactiverecord/test/base_test.rb6
3 files changed, 16 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index dd1c1fa0fe..9b1ac32f23 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Base.update_all :order and :limit options. Useful for MySQL updates that must be ordered to avoid violating unique constraints. [Jeremy Kemper]
+
* Remove deprecated object transactions. People relying on this functionality should install the object_transactions plugin at http://code.bitsweat.net/svn/object_transactions. Closes #5637 [Koz, Jeremy Kemper]
* PostgreSQL: remove DateTime -> Time downcast. Warning: do not enable translate_results for the C bindings if you have timestamps outside Time's domain. [Jeremy Kemper]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index cb50d64658..c680d7e304 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -501,9 +501,15 @@ module ActiveRecord #:nodoc:
# Updates all records with the SET-part of an SQL update statement in +updates+ and returns an integer with the number of rows updated.
# A subset of the records can be selected by specifying +conditions+. Example:
# Billing.update_all "category = 'authorized', approved = 1", "author = 'David'"
- def update_all(updates, conditions = nil)
+ #
+ # Optional :order and :limit options may be given as the third parameter,
+ # but their behavior is database-specific.
+ def update_all(updates, conditions = nil, options = {})
sql = "UPDATE #{table_name} SET #{sanitize_sql_for_assignment(updates)} "
- add_conditions!(sql, conditions, scope(:find))
+ scope = scope(:find)
+ add_conditions!(sql, conditions, scope)
+ add_order!(sql, options[:order], scope)
+ add_limit!(sql, options, scope)
connection.update(sql, "#{name} Update")
end
diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb
index cbc144f57a..c3ae9a08b9 100755
--- a/activerecord/test/base_test.rb
+++ b/activerecord/test/base_test.rb
@@ -569,6 +569,12 @@ class BasicsTest < Test::Unit::TestCase
end
end
+ if current_adapter?(:MysqlAdapter)
+ def test_update_all_with_order_and_limit
+ assert_equal 1, Topic.update_all("content = 'bulk updated!'", nil, :limit => 1, :order => 'id DESC')
+ end
+ end
+
def test_update_many
topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
updated = Topic.update(topic_data.keys, topic_data.values)