Skip to content

Rails 6: Updates for bulk insert/upsert #795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2020

Conversation

aidanharan
Copy link
Contributor

@aidanharan aidanharan commented Apr 29, 2020

Bulk inserts akin to the bulk updates provided by update_all and bulk deletes by delete_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+) and ON 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 the supports_insert_on_duplicate_skip?/supports_insert_on_duplicate_update? functionality before calling insert/upsert.

References:

@aidanharan aidanharan changed the title Rails 6: Add insert_many to ActiveRecord models Rails 6: Added insert returning functionality Apr 30, 2020
@aidanharan aidanharan changed the title Rails 6: Added insert returning functionality Rails 6: Updates for bulk insert/upsert Apr 30, 2020
@aidanharan aidanharan marked this pull request as ready for review April 30, 2020 12:50
true
end

def supports_insert_on_duplicate_skip?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are already set to false by the abstract adapter but think it better to duplicate the methods here so that it's clear what the MSSQL adapter supports.

@wpolicarpo wpolicarpo merged commit 3eba185 into rails-sqlserver:6-0-dev Apr 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants