mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Support string returning clause for AR#insert_all
(cherry picked from commit 15b3310a4d
)
This commit is contained in:
parent
9aed3dcdfe
commit
c7613dde53
4 changed files with 38 additions and 1 deletions
|
@ -1,3 +1,18 @@
|
|||
* Allow passing raw SQL as `returning` statement to `#upsert_all`:
|
||||
|
||||
```ruby
|
||||
Article.insert_all(
|
||||
[
|
||||
{title: "Article 1", slug: "article-1", published: false},
|
||||
{title: "Article 2", slug: "article-2", published: false}
|
||||
],
|
||||
# Some PostgreSQL magic here to detect which rows have been actually inserted
|
||||
returning: "id, (xmax = '0') as inserted, name as new_name"
|
||||
)
|
||||
```
|
||||
|
||||
*Vladimir Dementyev*
|
||||
|
||||
* Deprecate `legacy_connection_handling`.
|
||||
|
||||
*Eileen M. Uchitelle*
|
||||
|
|
|
@ -151,7 +151,13 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def returning
|
||||
format_columns(insert_all.returning) if insert_all.returning
|
||||
return unless insert_all.returning
|
||||
|
||||
if insert_all.returning.is_a?(String)
|
||||
insert_all.returning
|
||||
else
|
||||
format_columns(insert_all.returning)
|
||||
end
|
||||
end
|
||||
|
||||
def conflict_target
|
||||
|
|
|
@ -91,6 +91,9 @@ module ActiveRecord
|
|||
# or <tt>returning: false</tt> to omit the underlying <tt>RETURNING</tt> SQL
|
||||
# clause entirely.
|
||||
#
|
||||
# You can also pass an SQL string if you need more control on the return values
|
||||
# (for example, <tt>returning: "id, name as new_name"</tt>).
|
||||
#
|
||||
# [:unique_by]
|
||||
# (PostgreSQL and SQLite only) By default rows are considered to be unique
|
||||
# by every unique index on the table. Any duplicate rows are skipped.
|
||||
|
@ -168,6 +171,9 @@ module ActiveRecord
|
|||
# or <tt>returning: false</tt> to omit the underlying <tt>RETURNING</tt> SQL
|
||||
# clause entirely.
|
||||
#
|
||||
# You can also pass an SQL string if you need more control on the return values
|
||||
# (for example, <tt>returning: "id, name as new_name"</tt>).
|
||||
#
|
||||
# ==== Examples
|
||||
#
|
||||
# # Insert multiple records
|
||||
|
@ -216,6 +222,9 @@ module ActiveRecord
|
|||
# or <tt>returning: false</tt> to omit the underlying <tt>RETURNING</tt> SQL
|
||||
# clause entirely.
|
||||
#
|
||||
# You can also pass an SQL string if you need more control on the return values
|
||||
# (for example, <tt>returning: "id, name as new_name"</tt>).
|
||||
#
|
||||
# [:unique_by]
|
||||
# (PostgreSQL and SQLite only) By default rows are considered to be unique
|
||||
# by every unique index on the table. Any duplicate rows are skipped.
|
||||
|
|
|
@ -109,6 +109,13 @@ class InsertAllTest < ActiveRecord::TestCase
|
|||
assert_equal %w[ Rework ], result.pluck("name")
|
||||
end
|
||||
|
||||
def test_insert_all_returns_requested_sql_fields
|
||||
skip unless supports_insert_returning?
|
||||
|
||||
result = Book.insert_all! [{ name: "Rework", author_id: 1 }], returning: "UPPER(name) as name"
|
||||
assert_equal %w[ REWORK ], result.pluck("name")
|
||||
end
|
||||
|
||||
def test_insert_all_can_skip_duplicate_records
|
||||
skip unless supports_insert_on_duplicate_skip?
|
||||
|
||||
|
|
Loading…
Reference in a new issue