From 144e8691cbfb8bba77f18cfe68d5e7fd48887f5e Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 28 Sep 2012 17:55:35 +0100 Subject: Support for partial inserts. When inserting new records, only the fields which have been changed from the defaults will actually be included in the INSERT statement. The other fields will be populated by the database. This is more efficient, and also means that it will be safe to remove database columns without getting subsequent errors in running app processes (so long as the code in those processes doesn't contain any references to the removed column). --- activerecord/test/cases/dirty_test.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'activerecord/test/cases/dirty_test.rb') diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index 9a2a5a4e3c..75a52544d3 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -3,6 +3,7 @@ require 'models/topic' # For booleans require 'models/pirate' # For timestamps require 'models/parrot' require 'models/person' # For optimistic locking +require 'models/aircraft' class Pirate # Just reopening it, not defining it attr_accessor :detected_changes_in_after_update # Boolean for if changes are detected @@ -550,6 +551,30 @@ class DirtyTest < ActiveRecord::TestCase end end + test "partial insert" do + with_partial_updates Person do + jon = nil + assert_sql(/first_name/) do + jon = Person.create! first_name: 'Jon' + end + + assert ActiveRecord::SQLCounter.log_all.none? { |sql| sql =~ /followers_count/ } + + jon.reload + assert_equal 'Jon', jon.first_name + assert_equal 0, jon.followers_count + assert_not_nil jon.id + end + end + + test "partial insert with empty values" do + with_partial_updates Aircraft do + a = Aircraft.create! + a.reload + assert_not_nil a.id + end + end + private def with_partial_updates(klass, on = true) old = klass.partial_updates? -- cgit v1.2.3