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

Tidy up fix for PG extensions quoting

Always pass in the column for quote_bound_value and quote using it in
case it exists there.
This commit is contained in:
Carlos Antonio da Silva 2013-12-20 08:45:54 -02:00
parent 73bba4c1e1
commit 9e1740af9c
5 changed files with 26 additions and 12 deletions

View file

@ -1,9 +1,9 @@
* Fixed `update_column`, `update_columns`, and `update_all` to correctly serialize
values for `array`, `hstore` and `json` column types in `PostgreSQL`.
values for `array`, `hstore` and `json` column types in PostgreSQL.
Fixes #12261.
*Tadas Tamosauskas*
*Tadas Tamosauskas*, *Carlos Antonio da Silva*
* Do not consider PostgreSQL array columns as number or text columns.

View file

@ -100,8 +100,9 @@ module ActiveRecord
# { status: nil, group_id: 1 }
# # => "status = NULL , group_id = 1"
def sanitize_sql_hash_for_assignment(attrs, table)
c = connection
attrs.map do |attr, value|
"#{connection.quote_table_name_for_assignment(table, attr)} = #{quote_bound_value(value, connection, columns_hash[attr.to_s])}"
"#{c.quote_table_name_for_assignment(table, attr)} = #{quote_bound_value(value, c, columns_hash[attr.to_s])}"
end.join(', ')
end
@ -153,15 +154,16 @@ module ActiveRecord
end
def quote_bound_value(value, c = connection, column = nil) #:nodoc:
db_native_type = column && (column.respond_to?(:array) && column.array || [:json, :hstore].include?(column.type) )
if value.respond_to?(:map) && !value.acts_like?(:string) && !db_native_type
if column
c.quote(value, column)
elsif value.respond_to?(:map) && !value.acts_like?(:string)
if value.respond_to?(:empty?) && value.empty?
c.quote(nil)
else
value.map { |v| c.quote(v) }.join(',')
end
else
c.quote(value, column)
c.quote(value)
end
end

View file

@ -129,9 +129,13 @@ class PostgresqlArrayTest < ActiveRecord::TestCase
end
def test_update_all
PgArray.create! tags: ["one", "two", "three"]
pg_array = PgArray.create! tags: ["one", "two", "three"]
PgArray.update_all tags: ["four", "five"]
assert_equal ["four", "five"], PgArray.first.tags
assert_equal ["four", "five"], pg_array.reload.tags
PgArray.update_all tags: []
assert_equal [], pg_array.reload.tags
end
private

View file

@ -208,9 +208,13 @@ class PostgresqlHstoreTest < ActiveRecord::TestCase
end
def test_update_all
Hstore.create! tags: { "one" => "two" }
hstore = Hstore.create! tags: { "one" => "two" }
Hstore.update_all tags: { "three" => "four" }
assert_equal({ "three" => "four" }, Hstore.first.tags)
assert_equal({ "three" => "four" }, hstore.reload.tags)
Hstore.update_all tags: { }
assert_equal({ }, hstore.reload.tags)
end
end

View file

@ -123,8 +123,12 @@ class PostgresqlJSONTest < ActiveRecord::TestCase
end
def test_update_all
JsonDataType.create! payload: { "one" => "two" }
json = JsonDataType.create! payload: { "one" => "two" }
JsonDataType.update_all payload: { "three" => "four" }
assert_equal({ "three" => "four" }, JsonDataType.first.payload)
assert_equal({ "three" => "four" }, json.reload.payload)
JsonDataType.update_all payload: { }
assert_equal({ }, json.reload.payload)
end
end