aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2019-03-06 16:29:22 +0100
committerKasper Timm Hansen <kaspth@gmail.com>2019-03-06 16:29:22 +0100
commit1818c4e8b49d053ca7a220288dd6823984bc0328 (patch)
treef21122b0708f60eeca19ad5073f313f595c61bba /activerecord
parent525e7720d04e4dba8a4cf9fb289799cc4f230f30 (diff)
downloadrails-1818c4e8b49d053ca7a220288dd6823984bc0328.tar.gz
rails-1818c4e8b49d053ca7a220288dd6823984bc0328.tar.bz2
rails-1818c4e8b49d053ca7a220288dd6823984bc0328.zip
Add some whitespace for readability.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/insert_all.rb2
-rw-r--r--activerecord/test/cases/insert_all_test.rb12
2 files changed, 14 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/insert_all.rb b/activerecord/lib/active_record/insert_all.rb
index 6d14ff70b6..5baaea344b 100644
--- a/activerecord/lib/active_record/insert_all.rb
+++ b/activerecord/lib/active_record/insert_all.rb
@@ -8,8 +8,10 @@ module ActiveRecord
raise ArgumentError, "Empty list of attributes passed" if inserts.blank?
@model, @connection, @inserts, @on_duplicate, @returning, @unique_by = model, model.connection, inserts, on_duplicate, returning, unique_by
+
@returning = (connection.supports_insert_returning? ? primary_keys : false) if @returning.nil?
@returning = false if @returning == []
+
@on_duplicate = :skip if @on_duplicate == :update && updatable_columns.empty?
ensure_valid_options_for_connection!
diff --git a/activerecord/test/cases/insert_all_test.rb b/activerecord/test/cases/insert_all_test.rb
index a396689db6..63245719c0 100644
--- a/activerecord/test/cases/insert_all_test.rb
+++ b/activerecord/test/cases/insert_all_test.rb
@@ -56,30 +56,35 @@ class InsertAllTest < ActiveRecord::TestCase
def test_insert_all_returns_primary_key_if_returning_is_supported
skip unless supports_insert_returning?
+
result = Book.insert_all! [{ name: "Rework", author_id: 1 }]
assert_equal %w[ id ], result.columns
end
def test_insert_all_returns_nothing_if_returning_is_empty
skip unless supports_insert_returning?
+
result = Book.insert_all! [{ name: "Rework", author_id: 1 }], returning: []
assert_equal [], result.columns
end
def test_insert_all_returns_nothing_if_returning_is_false
skip unless supports_insert_returning?
+
result = Book.insert_all! [{ name: "Rework", author_id: 1 }], returning: false
assert_equal [], result.columns
end
def test_insert_all_returns_requested_fields
skip unless supports_insert_returning?
+
result = Book.insert_all! [{ name: "Rework", author_id: 1 }], returning: [:id, :name]
assert_equal %w[ Rework ], result.pluck("name")
end
def test_insert_all_can_skip_duplicate_records
skip unless supports_insert_on_duplicate_skip?
+
assert_no_difference "Book.count" do
Book.insert_all [{ id: 1, name: "Agile Web Development with Rails" }]
end
@@ -87,6 +92,7 @@ class InsertAllTest < ActiveRecord::TestCase
def test_insert_all_will_raise_if_duplicates_are_skipped_only_for_a_certain_conflict_target
skip unless supports_insert_on_duplicate_skip? && supports_insert_conflict_target?
+
assert_raise ActiveRecord::RecordNotUnique do
Book.insert_all [{ id: 1, name: "Agile Web Development with Rails" }],
unique_by: { columns: %i{author_id name} }
@@ -95,6 +101,7 @@ class InsertAllTest < ActiveRecord::TestCase
def test_upsert_all_updates_existing_records
skip unless supports_insert_on_duplicate_update?
+
new_name = "Agile Web Development with Rails, 4th Edition"
Book.upsert_all [{ id: 1, name: new_name }]
assert_equal new_name, Book.find(1).name
@@ -102,6 +109,7 @@ class InsertAllTest < ActiveRecord::TestCase
def test_upsert_all_does_not_update_readonly_attributes
skip unless supports_insert_on_duplicate_update?
+
new_name = "Agile Web Development with Rails, 4th Edition"
ReadonlyNameBook.upsert_all [{ id: 1, name: new_name }]
assert_not_equal new_name, Book.find(1).name
@@ -109,9 +117,11 @@ class InsertAllTest < ActiveRecord::TestCase
def test_upsert_all_does_not_update_primary_keys
skip unless supports_insert_on_duplicate_update? && supports_insert_conflict_target?
+
Book.upsert_all [{ id: 101, name: "Perelandra", author_id: 7 }]
Book.upsert_all [{ id: 103, name: "Perelandra", author_id: 7, isbn: "1974522598" }],
unique_by: { columns: %i{author_id name} }
+
book = Book.find_by(name: "Perelandra")
assert_equal 101, book.id, "Should not have updated the ID"
assert_equal "1974522598", book.isbn, "Should have updated the isbn"
@@ -119,9 +129,11 @@ class InsertAllTest < ActiveRecord::TestCase
def test_upsert_all_does_not_perform_an_upsert_if_a_partial_index_doesnt_apply
skip unless supports_insert_on_duplicate_update? && supports_insert_conflict_target? && supports_partial_index?
+
Book.upsert_all [{ name: "Out of the Silent Planet", author_id: 7, isbn: "1974522598", published_on: Date.new(1938, 4, 1) }]
Book.upsert_all [{ name: "Perelandra", author_id: 7, isbn: "1974522598" }],
unique_by: { columns: %w[ isbn ], where: "published_on IS NOT NULL" }
+
assert_equal ["Out of the Silent Planet", "Perelandra"], Book.where(isbn: "1974522598").order(:name).pluck(:name)
end
end