1
0
Fork 0
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

This commit is contained in:
Vladimir Dementyev 2019-03-15 11:18:45 -04:00
parent 2c3332cc4c
commit 15b3310a4d
No known key found for this signature in database
GPG key ID: 8E0A19D3D1EDF5EB
3 changed files with 23 additions and 1 deletions

View file

@ -135,7 +135,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

View file

@ -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.
@ -160,6 +163,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
@ -208,6 +214,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.

View file

@ -96,6 +96,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?