-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Add ability to provide raw SQL as returning and update to #upsert_all #41933
Add ability to provide raw SQL as returning and update to #upsert_all #41933
Conversation
(cherry picked from commit 15b3310)
1b262d2
to
3d2c8a5
Compare
3d2c8a5
to
8f3c12f
Compare
activerecord/CHANGELOG.md
Outdated
```ruby | ||
Book.upsert_all( | ||
[{ id: 1, status: 1 }, { id: 2, status: 1 }], | ||
update_sql: "status = GREATEST(books.status, EXCLUDED.status)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of update_sql
does it makes sense to use the on_duplicated
option? If it is a string we use it as the SQL to update on conflict.
activerecord/CHANGELOG.md
Outdated
```ruby | ||
Book.upsert_all( | ||
[{ id: 1, status: 1 }, { id: 2, status: 1 }], | ||
update_sql: "status = GREATEST(books.status, EXCLUDED.status)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And to avoid SQL injection we just only accept Arel.sql
both here an in the returning
option.
rails#41933 added a new `on_duplicate:` option to `upsert_all`, to allow providing custom SQL update code. This change makes `on_duplicate` admit an array of columns too, so that `upsert_all` only updates those columns when a conflict happens. This allows limiting the list of updated column in a database-agnostic way.
#41933 added a new `on_duplicate:` option to `upsert_all`, to allow providing custom SQL update code. This change makes `on_duplicate` admit an array of columns too, so that `upsert_all` only updates those columns when a conflict happens. This allows limiting the list of updated column in a database-agnostic way.
Revamp of #36636.
Summary
Original discussion: #35077 (comment)
returning:
option:update_sql:
option to specify SQL fragment to use when updating rows on conflict:Another example by
@boblail:
/cc @rafaelfranca