Skip to content

Commit

Permalink
Partial updates include only unsaved attributes. Off by default; set …
Browse files Browse the repository at this point in the history
…YourClass.partial_updates = true to enable.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9157 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Mar 31, 2008
1 parent 2cf72ad commit 6b9448c
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 34 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*SVN*

* Partial updates include only unsaved attributes. Off by default; set YourClass.partial_updates = true to enable. [Jeremy Kemper]

* Removing unnecessary uses_tzinfo helper from tests, given that TZInfo is now bundled [Geoff Buesing]

* Fixed that validates_size_of :within works in associations #11295, #10019 [cavalle]
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/aggregations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ def writer_method(name, class_name, mapping, allow_nil, conversion)
module_eval do
define_method("#{name}=") do |part|
if part.nil? && allow_nil
mapping.each { |pair| @attributes[pair.first] = nil }
mapping.each { |pair| self[pair.first] = nil }
instance_variable_set("@#{name}", nil)
else
part = conversion.call(part) unless part.is_a?(class_name.constantize) || conversion.nil?
mapping.each { |pair| @attributes[pair.first] = part.send(pair.last) }
mapping.each { |pair| self[pair.first] = part.send(pair.last) }
instance_variable_set("@#{name}", part.freeze)
end
end
Expand Down
8 changes: 4 additions & 4 deletions activerecord/lib/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2407,8 +2407,8 @@ def create_or_update

# Updates the associated record with values matching those of the instance attributes.
# Returns the number of affected rows.
def update
quoted_attributes = attributes_with_quotes(false, false)
def update(attribute_names = @attributes.keys)
quoted_attributes = attributes_with_quotes(false, false, attribute_names)
return 0 if quoted_attributes.empty?
connection.update(
"UPDATE #{self.class.quoted_table_name} " +
Expand Down Expand Up @@ -2500,10 +2500,10 @@ def attributes_protected_by_default

# Returns a copy of the attributes hash where all the values have been safely quoted for use in
# an SQL statement.
def attributes_with_quotes(include_primary_key = true, include_readonly_attributes = true)
def attributes_with_quotes(include_primary_key = true, include_readonly_attributes = true, attribute_names = @attributes.keys)
quoted = {}
connection = self.class.connection
@attributes.each_pair do |name, value|
attribute_names.each do |name|
if column = column_for_attribute(name)
quoted[name] = connection.quote(read_attribute(name), column) unless !include_primary_key && column.primary
end
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ def before_update() end
# Is called _after_ <tt>Base.save</tt> on existing objects that have a record.
def after_update() end

def update_with_callbacks #:nodoc:
def update_with_callbacks(*args) #:nodoc:
return false if callback(:before_update) == false
result = update_without_callbacks
result = update_without_callbacks(*args)
callback(:after_update)
result
end
Expand Down
52 changes: 36 additions & 16 deletions activerecord/lib/active_record/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@ module ActiveRecord
# person.name = 'bob'
# person.changed # => ['name']
# person.changes # => { 'name' => ['Bill', 'bob'] }
#
# Before modifying an attribute in-place:
# person.name_will_change!
# person.name << 'by'
# person.name_change # => ['uncle bob', 'uncle bobby']
module Dirty
def self.included(base)
base.attribute_method_suffix '_changed?', '_change', '_was'
base.attribute_method_suffix '_changed?', '_change', '_will_change!', '_was'
base.alias_method_chain :write_attribute, :dirty
base.alias_method_chain :save, :dirty
base.alias_method_chain :save!, :dirty
base.alias_method_chain :update, :dirty

base.superclass_delegating_accessor :partial_updates
base.partial_updates = true
end

# Do any attributes have unsaved changes?
Expand Down Expand Up @@ -81,14 +90,33 @@ def changed_attributes
@changed_attributes ||= {}
end

# Handle *_changed? for method_missing.
def attribute_changed?(attr)
changed_attributes.include?(attr)
end

# Handle *_change for method_missing.
def attribute_change(attr)
[changed_attributes[attr], __send__(attr)] if attribute_changed?(attr)
end

# Handle *_was for method_missing.
def attribute_was(attr)
attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr)
end

# Handle *_will_change! for method_missing.
def attribute_will_change!(attr)
changed_attributes[attr] = clone_attribute_value(:read_attribute, attr)
end

# Wrap write_attribute to remember original attribute value.
def write_attribute_with_dirty(attr, value)
attr = attr.to_s

# The attribute already has an unsaved change.
unless changed_attributes.include?(attr)
old = read_attribute(attr)
old = clone_attribute_value(:read_attribute, attr)

# Remember the original value if it's different.
typecasted = if column = column_for_attribute(attr)
Expand All @@ -103,20 +131,12 @@ def write_attribute_with_dirty(attr, value)
write_attribute_without_dirty(attr, value)
end


# Handle *_changed? for method_missing.
def attribute_changed?(attr)
changed_attributes.include?(attr)
end

# Handle *_change for method_missing.
def attribute_change(attr)
[changed_attributes[attr], __send__(attr)] if attribute_changed?(attr)
end

# Handle *_was for method_missing.
def attribute_was(attr)
attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr)
def update_with_dirty
if partial_updates?
update_without_dirty(changed)
else
update_without_dirty
end
end
end
end
9 changes: 6 additions & 3 deletions activerecord/lib/active_record/locking/optimistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,20 @@ def attributes_from_column_definition_with_lock
return result
end

def update_with_lock #:nodoc:
return update_without_lock unless locking_enabled?
def update_with_lock(attribute_names = @attributes.keys) #:nodoc:
return update_without_lock(attribute_names) unless locking_enabled?

lock_col = self.class.locking_column
previous_value = send(lock_col).to_i
send(lock_col + '=', previous_value + 1)

attribute_names += [lock_col]
attribute_names.uniq!

begin
affected_rows = connection.update(<<-end_sql, "#{self.class.name} Update with optimistic locking")
UPDATE #{self.class.table_name}
SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false, false))}
SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false, false, attribute_names))}
WHERE #{self.class.primary_key} = #{quote_value(id)}
AND #{self.class.quoted_locking_column} = #{quote_value(previous_value)}
end_sql
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/timestamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ def create_with_timestamps #:nodoc:
create_without_timestamps
end

def update_with_timestamps #:nodoc:
def update_with_timestamps(*args) #:nodoc:
if record_timestamps
t = self.class.default_timezone == :utc ? Time.now.utc : Time.now
write_attribute('updated_at', t) if respond_to?(:updated_at)
write_attribute('updated_on', t) if respond_to?(:updated_on)
end
update_without_timestamps
update_without_timestamps(*args)
end
end
end
3 changes: 2 additions & 1 deletion activerecord/test/cases/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,12 @@ def test_assignment_before_either_saved

def test_not_resaved_when_unchanged
firm = Firm.find(:first, :include => :account)
firm.name += '-changed'
assert_queries(1) { firm.save! }

firm = Firm.find(:first)
firm.account = Account.find(:first)
assert_queries(1) { firm.save! }
assert_queries(Firm.partial_updates? ? 0 : 1) { firm.save! }

firm = Firm.find(:first).clone
firm.account = Account.find(:first)
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/cases/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def test_hash_content

assert_equal 2, Topic.find(topic.id).content["two"]

topic.content_will_change!
topic.content["three"] = 3
topic.save

Expand Down
47 changes: 45 additions & 2 deletions activerecord/test/cases/dirty_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'cases/helper'
require 'models/topic' # For booleans
require 'models/pirate' # For timestamps
require 'models/parrot'

class Pirate # Just reopening it, not defining it
attr_accessor :detected_changes_in_after_update # Boolean for if changes are detected
Expand All @@ -19,7 +20,7 @@ def check_changes
end
end

class DirtyTest < Test::Unit::TestCase
class DirtyTest < ActiveRecord::TestCase
def test_attribute_changes
# New record - no changes.
pirate = Pirate.new
Expand All @@ -43,7 +44,6 @@ def test_attribute_changes
assert_nil pirate.catchphrase_change
end

# Rewritten from original tests to use AR
def test_object_should_be_changed_if_any_attribute_is_changed
pirate = Pirate.new
assert !pirate.changed?
Expand All @@ -62,6 +62,28 @@ def test_object_should_be_changed_if_any_attribute_is_changed
assert_equal Hash.new, pirate.changes
end

def test_attribute_will_change!
pirate = Pirate.create!(:catchphrase => 'arr')

pirate.catchphrase << ' matey'
assert !pirate.catchphrase_changed?

assert pirate.catchphrase_will_change!
assert pirate.catchphrase_changed?
assert_equal ['arr matey', 'arr matey'], pirate.catchphrase_change

pirate.catchphrase << '!'
assert pirate.catchphrase_changed?
assert_equal ['arr matey', 'arr matey!'], pirate.catchphrase_change
end

def test_association_assignment_changes_foreign_key
pirate = Pirate.create!
pirate.parrot = Parrot.create!
assert pirate.changed?
assert_equal %w(parrot_id), pirate.changed
end

def test_attribute_should_be_compared_with_type_cast
topic = Topic.new
assert topic.approved?
Expand All @@ -74,4 +96,25 @@ def test_attribute_should_be_compared_with_type_cast
assert topic.approved?
assert !topic.approved_changed?
end

def test_partial_update
pirate = Pirate.new(:catchphrase => 'foo')

with_partial_updates Pirate, false do
assert_queries(2) { 2.times { pirate.save! } }
end

with_partial_updates Pirate, true do
assert_queries(0) { 2.times { pirate.save! } }
end
end

private
def with_partial_updates(klass, on = true)
old = klass.partial_updates?
klass.partial_updates = on
yield
ensure
klass.partial_updates = old
end
end
4 changes: 3 additions & 1 deletion activerecord/test/cases/query_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ def test_update
Task.connection.expects(:clear_query_cache).times(2)

Task.cache do
Task.find(1).save!
task = Task.find(1)
task.starting = Time.now.utc
task.save!
end
end

Expand Down
5 changes: 5 additions & 0 deletions railties/configs/initializers/new_in_rails_3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# These settins change the behavior of Rails 2 apps and will be defaults
# for Rails 3. You can remove this initializer when Rails 3 is released.

# Only save the attributes that have changed since the record was loaded.
ActiveRecord::Base.partial_updates = true
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def manifest

# Initializers
m.template "configs/initializers/inflections.rb", "config/initializers/inflections.rb"
m.template "configs/initializers/mime_types.rb", "config/initializers/mime_types.rb"
m.template "configs/initializers/mime_types.rb", "config/initializers/mime_types.rb"
m.template "configs/initializers/new_in_rails_3.rb", "config/initializers/new_in_rails_3.rb"

# Environments
m.file "environments/boot.rb", "config/boot.rb"
Expand Down

0 comments on commit 6b9448c

Please sign in to comment.