1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix PostgreSQL insert to properly extract table name from multiline string SQL.

Previously, executing an insert SQL in PostgreSQL with a command like this:

    insert into articles(
      number)
    values(
      5152
    )

would not work because the adapter was unable to extract the correct articles table name.
This commit is contained in:
Kuldeep Aggarwal 2013-12-19 18:46:17 +05:30
parent 3de199988f
commit b082bece5e
3 changed files with 28 additions and 1 deletions

View file

@ -1,3 +1,18 @@
* Fix `PostgreSQL` insert to properly extract table name from multiline string SQL.
Previously, executing an insert SQL in `PostgreSQL` with a command like this:
insert into articles(
number)
values(
5152
)
would not work because the adapter was unable to extract the correct `articles`
table name.
*Kuldeep Aggarwal*
* `Relation` no longer has mutator methods like `#map!` and `#delete_if`. Convert
to an `Array` by calling `#to_a` before using these methods.

View file

@ -969,7 +969,7 @@ module ActiveRecord
end
def extract_table_ref_from_insert_sql(sql)
sql[/into\s+([^\(]*).*values\s*\(/i]
sql[/into\s+([^\(]*).*values\s*\(/im]
$1.strip if $1
end

View file

@ -62,6 +62,18 @@ module ActiveRecord
assert_equal expect, id
end
def test_multiline_insert_sql
id = @connection.insert_sql(<<-SQL)
insert into ex(
number)
values(
5152
)
SQL
expect = @connection.query('select max(id) from ex').first.first
assert_equal expect, id
end
def test_insert_sql_with_returning_disabled
connection = connection_without_insert_returning
id = connection.insert_sql("insert into postgresql_partitioned_table_parent (number) VALUES (1)")