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:
parent
3de199988f
commit
b082bece5e
3 changed files with 28 additions and 1 deletions
|
@ -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
|
* `Relation` no longer has mutator methods like `#map!` and `#delete_if`. Convert
|
||||||
to an `Array` by calling `#to_a` before using these methods.
|
to an `Array` by calling `#to_a` before using these methods.
|
||||||
|
|
||||||
|
|
|
@ -969,7 +969,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract_table_ref_from_insert_sql(sql)
|
def extract_table_ref_from_insert_sql(sql)
|
||||||
sql[/into\s+([^\(]*).*values\s*\(/i]
|
sql[/into\s+([^\(]*).*values\s*\(/im]
|
||||||
$1.strip if $1
|
$1.strip if $1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -62,6 +62,18 @@ module ActiveRecord
|
||||||
assert_equal expect, id
|
assert_equal expect, id
|
||||||
end
|
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
|
def test_insert_sql_with_returning_disabled
|
||||||
connection = connection_without_insert_returning
|
connection = connection_without_insert_returning
|
||||||
id = connection.insert_sql("insert into postgresql_partitioned_table_parent (number) VALUES (1)")
|
id = connection.insert_sql("insert into postgresql_partitioned_table_parent (number) VALUES (1)")
|
||||||
|
|
Loading…
Reference in a new issue