Permalink
Browse files

cast hstore values on write to be consistent with reading from the db.

  • Loading branch information...
senny committed Aug 7, 2013
1 parent 765b8aa commit 5ac2341fab689344991b2a4817bd2bc8b3edac9d
View
@@ -1,3 +1,18 @@
* Type cast hstore values on write, so that the value is consistent
with reading from the database.
Example:
x = Hstore.new tags: {"bool" => true, "number" => 5}
# Before:
x.tags # => {"bool" => true, "number" => 5}
# After:
x.tags # => {"bool" => "true", "number" => "5"}
*Yves Senn* , *Severin Schoepke*
* Fix multidimensional PG arrays containing non-string items.
*Yves Senn*
@@ -6,10 +6,6 @@ class PostgreSQLAdapter < AbstractAdapter
module OID
class Type
def type; end
def type_cast_for_write(value)
value
end
end
class Identity < Type
@@ -224,6 +220,10 @@ def type_cast(value)
end
class Hstore < Type
def type_cast_for_write(value)
ConnectionAdapters::PostgreSQLColumn.hstore_to_string value
end
def type_cast(value)
return if value.nil?
@@ -129,6 +129,14 @@ def self.extract_value_from_default(default)
end
end
def type_cast_for_write(value)
if @oid_type.respond_to?(:type_cast_for_write)
@oid_type.type_cast_for_write(value)
else
super
end
end
def type_cast(value)
return if value.nil?
return super if encoded?
@@ -70,6 +70,13 @@ def test_change_table_supports_hstore
Hstore.reset_column_information
end
def test_cast_value_on_write
x = Hstore.new tags: {"bool" => true, "number" => 5}
assert_equal({"bool" => "true", "number" => "5"}, x.tags)
x.save
assert_equal({"bool" => "true", "number" => "5"}, x.reload.tags)
end
def test_type_cast_hstore
assert @column

10 comments on commit 5ac2341

@senny

This comment has been minimized.

Show comment
Hide comment
@senny
Member

senny replied Aug 8, 2013

@tenderlove

This comment has been minimized.

Show comment
Hide comment
@tenderlove

tenderlove Aug 8, 2013

Member

👍 seems legit.

Member

tenderlove replied Aug 8, 2013

👍 seems legit.

@inopinatus

This comment has been minimized.

Show comment
Hide comment
@inopinatus

inopinatus Oct 6, 2013

Contributor

git-bisect is telling me that this commit broke store_accessor for hstores.

Test code at https://gist.github.com/inopinatus/6850497

Contributor

inopinatus replied Oct 6, 2013

git-bisect is telling me that this commit broke store_accessor for hstores.

Test code at https://gist.github.com/inopinatus/6850497

@senny

This comment has been minimized.

Show comment
Hide comment
@senny

senny Oct 7, 2013

Member

@inopinatus I can confirm your problem.

The issue is that after the patch writing and reading an hstore column always results in a Hash. Even when you tried to set a HashWithIndifferentAccess. As store.rb needs a HashWithIndifferentAccess to work properly things go bad.

I'll look into a possible solution.

Member

senny replied Oct 7, 2013

@inopinatus I can confirm your problem.

The issue is that after the patch writing and reading an hstore column always results in a Hash. Even when you tried to set a HashWithIndifferentAccess. As store.rb needs a HashWithIndifferentAccess to work properly things go bad.

I'll look into a possible solution.

@ffortin

This comment has been minimized.

Show comment
Hide comment
@ffortin

ffortin Sep 24, 2014

@senny Any updates on this? The problem raised by @inopinatus is still broken in 4.1 as it is.

ffortin replied Sep 24, 2014

@senny Any updates on this? The problem raised by @inopinatus is still broken in 4.1 as it is.

@senny

This comment has been minimized.

Show comment
Hide comment
@senny

senny Sep 24, 2014

Member

@ffortin just checked and the test script above passes on both master and 4-1-stable.

Member

senny replied Sep 24, 2014

@ffortin just checked and the test script above passes on both master and 4-1-stable.

@ffortin

This comment has been minimized.

Show comment
Hide comment
@ffortin

ffortin Sep 24, 2014

@senny My bad, I've published an updated gist, the problem happens when duplicating an object with hstore properties. Might be worth filing under a separate issue?

See: https://gist.github.com/ffortin/04469945cc2e47a0c56c

This used to work under 4.0

ffortin replied Sep 24, 2014

@senny My bad, I've published an updated gist, the problem happens when duplicating an object with hstore properties. Might be worth filing under a separate issue?

See: https://gist.github.com/ffortin/04469945cc2e47a0c56c

This used to work under 4.0

@senny

This comment has been minimized.

Show comment
Hide comment
@senny

senny Sep 24, 2014

Member

@ffortin I am still not able to reproduce. I ran the gist against master and 4-1-stable and it passes...

Member

senny replied Sep 24, 2014

@ffortin I am still not able to reproduce. I ran the gist against master and 4-1-stable and it passes...

@ffortin

This comment has been minimized.

Show comment
Hide comment
@ffortin

ffortin Sep 24, 2014

@senny Ok sorry, we were testing with 4.1.5, it was resolved in 4.1.6, by you in fact.

"Keep PostgreSQL hstore and json attributes as Hash in @attributes. Fixes duplication in combination with store_accessor.

Fixes #15369.

Yves Senn"

Thanks and sorry for bothering you!

ffortin replied Sep 24, 2014

@senny Ok sorry, we were testing with 4.1.5, it was resolved in 4.1.6, by you in fact.

"Keep PostgreSQL hstore and json attributes as Hash in @attributes. Fixes duplication in combination with store_accessor.

Fixes #15369.

Yves Senn"

Thanks and sorry for bothering you!

@senny

This comment has been minimized.

Show comment
Hide comment
@senny

senny Sep 24, 2014

Member

@ffortin thanks for confirming 😁

Member

senny replied Sep 24, 2014

@ffortin thanks for confirming 😁

Please sign in to comment.