diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2019-04-08 23:54:07 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-08 23:54:07 +0900 |
commit | 652ce83a4d8882cc964dbe168c9ca8ea67096810 (patch) | |
tree | 685b5d546937dc8f48418b0d31133980e785088d /activerecord/test | |
parent | dbd3ce616076fc5688cea67e124676ee504bcb1b (diff) | |
parent | c24efdbd2720bd4d936a3ffab0c22af0a718d2f1 (diff) | |
download | rails-652ce83a4d8882cc964dbe168c9ca8ea67096810.tar.gz rails-652ce83a4d8882cc964dbe168c9ca8ea67096810.tar.bz2 rails-652ce83a4d8882cc964dbe168c9ca8ea67096810.zip |
Merge pull request #35854 from boblail/fix-bug-with-insert_all-on-mysql
When skipping duplicates in bulk insert on MySQL, avoid assigning an AUTONUMBER column when not specified
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/insert_all_test.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/activerecord/test/cases/insert_all_test.rb b/activerecord/test/cases/insert_all_test.rb index fc25701c80..61fbda1f0f 100644 --- a/activerecord/test/cases/insert_all_test.rb +++ b/activerecord/test/cases/insert_all_test.rb @@ -104,6 +104,44 @@ class InsertAllTest < ActiveRecord::TestCase end end + def test_insert_all_with_skip_duplicates_and_autonumber_id_not_given + skip unless supports_insert_on_duplicate_skip? + + assert_difference "Book.count", 1 do + # These two books are duplicates according to an index on %i[author_id name] + # but their IDs are not specified so they will be assigned different IDs + # by autonumber. We will get an exception from MySQL if we attempt to skip + # one of these records by assigning its ID. + Book.insert_all [ + { author_id: 8, name: "Refactoring" }, + { author_id: 8, name: "Refactoring" } + ] + end + end + + def test_insert_all_with_skip_duplicates_and_autonumber_id_given + skip unless supports_insert_on_duplicate_skip? + + assert_difference "Book.count", 1 do + Book.insert_all [ + { id: 200, author_id: 8, name: "Refactoring" }, + { id: 201, author_id: 8, name: "Refactoring" } + ] + end + end + + def test_skip_duplicates_strategy_does_not_secretly_upsert + skip unless supports_insert_on_duplicate_skip? + + book = Book.create!(author_id: 8, name: "Refactoring", format: "EXPECTED") + + assert_no_difference "Book.count" do + Book.insert(author_id: 8, name: "Refactoring", format: "UNEXPECTED") + end + + assert_equal "EXPECTED", book.reload.format + end + 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? |