Rails 6: Updates for bulk insert/upsert #795
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Bulk inserts akin to the bulk updates provided by
update_all
and bulk deletes bydelete_all
was added to Rails in rails/rails#35077.Other adapters support skipping or upserting duplicates through the
ON CONFLICT
syntax for Postgres (9.5+) and Sqlite (3.24+) andON DUPLICATE KEY UPDATE
syntax for MySQL.SQL Server, however, does not support skipping or upserting duplicates. There are some workarounds in https://michaeljswart.com/2017/07/sql-server-upsert-patterns-and-antipatterns/ but they will require more work to implement and test.
This PR implements the functionality to support insert returning. This will run the previous skipped tests such as https://github.com/rails/rails/blob/master/activerecord/test/cases/insert_all_test.rb#L75
I have coerced the
QueryCacheExpiryTest#test_insert_all
test as it should have been checking whether the adapter supported thesupports_insert_on_duplicate_skip?
/supports_insert_on_duplicate_update?
functionality before callinginsert
/upsert
.References: