Skip to content
Permalink
Browse files
cast hstore values on write to be consistent with reading from the db.
  • Loading branch information
senny committed Aug 8, 2013
1 parent 765b8aa commit 5ac2341fab689344991b2a4817bd2bc8b3edac9d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
@@ -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
Copy link
Member Author

@senny senny commented on 5ac2341 Aug 8, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tenderlove
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 seems legit.

@inopinatus
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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

@senny
Copy link
Member Author

@senny senny commented on 5ac2341 Oct 7, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@senny
Copy link
Member Author

@senny senny commented on 5ac2341 Sep 24, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@ffortin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link
Member Author

@senny senny commented on 5ac2341 Sep 24, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@ffortin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link
Member Author

@senny senny commented on 5ac2341 Sep 24, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ffortin thanks for confirming 😁

Please sign in to comment.