Skip to content

Commit fb00295

Browse files
committed
[postgres] we should return "raw" hstore values on AR < 4.0 by default
(regression caused by fixing #454 for AR >= 4.0)
1 parent 35c88e1 commit fb00295

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

lib/arjdbc/postgresql/adapter.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,5 +1386,9 @@ def jdbc_column_class
13861386
::ActiveRecord::ConnectionAdapters::PostgreSQLColumn
13871387
end
13881388

1389+
if ActiveRecord::VERSION::MAJOR < 4 # Rails 3.x compatibility
1390+
PostgreSQLJdbcConnection.raw_hstore_type = true if PostgreSQLJdbcConnection.raw_hstore_type? == nil
1391+
end
1392+
13891393
end
13901394
end

src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ protected IRubyObject objectToRuby(final ThreadContext context,
499499
}
500500

501501
if ( object instanceof Map ) { // hstore
502-
if ( rawHstoreType ) {
502+
if ( rawHstoreType == Boolean.TRUE ) {
503503
return runtime.newString( resultSet.getString(column) );
504504
}
505505
// by default we avoid double parsing by driver and than column :
@@ -552,20 +552,25 @@ private String formatInterval(final Object object) {
552552
return str.toString();
553553
}
554554

555-
protected static boolean rawHstoreType = Boolean.getBoolean("arjdbc.postgresql.hstore.raw");
555+
protected static Boolean rawHstoreType;
556+
static {
557+
final String hstoreRaw = System.getProperty("arjdbc.postgresql.hstore.raw");
558+
if ( hstoreRaw != null ) rawHstoreType = Boolean.parseBoolean(hstoreRaw);
559+
}
556560

557561
@JRubyMethod(name = "raw_hstore_type?", meta = true)
558562
public static IRubyObject useRawHstoreType(final ThreadContext context, final IRubyObject self) {
563+
if ( rawHstoreType == null ) return context.getRuntime().getNil();
559564
return context.getRuntime().newBoolean(rawHstoreType);
560565
}
561566

562567
@JRubyMethod(name = "raw_hstore_type=", meta = true)
563568
public static IRubyObject setRawHstoreType(final IRubyObject self, final IRubyObject value) {
564569
if ( value instanceof RubyBoolean ) {
565-
rawHstoreType = ((RubyBoolean) value).isTrue();
570+
rawHstoreType = ((RubyBoolean) value).isTrue() ? Boolean.TRUE : Boolean.FALSE;
566571
}
567572
else {
568-
rawHstoreType = ! value.isNil();
573+
rawHstoreType = value.isNil() ? null : Boolean.TRUE;
569574
}
570575
return value;
571576
}

test/db/postgresql/hstore_test.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
require 'test_helper'
33
require 'db/postgres'
44

5-
class PostgresqlHstoreTest < Test::Unit::TestCase
5+
# Rails 4.x test
6+
class PostgreSQLHstoreTest < Test::Unit::TestCase
67

78
class Hstore < ActiveRecord::Base
89
self.table_name = 'hstores'
@@ -212,3 +213,40 @@ def assert_cycle hash
212213
end
213214

214215
end if Test::Unit::TestCase.ar_version('4.0')
216+
217+
# Rails <= 3.2 test
218+
class PostgreSQKHstoreTest < Test::Unit::TestCase
219+
220+
class Hstore < ActiveRecord::Base
221+
self.table_name = 'hstores'
222+
end
223+
224+
def setup
225+
@connection = ActiveRecord::Base.connection
226+
227+
unless @connection.supports_extensions?
228+
return skip "do not test on PG without hstore"
229+
end
230+
231+
unless @connection.extension_enabled?('hstore')
232+
@connection.enable_extension 'hstore'
233+
@connection.commit_db_transaction
234+
end
235+
236+
@connection.reconnect!
237+
238+
@connection.execute 'CREATE TABLE "hstores" ("id" serial primary key, "tags" hstore DEFAULT \'\')'
239+
end
240+
241+
def teardown
242+
@connection.execute 'DROP TABLE IF EXISTS "hstores"'
243+
end
244+
245+
test 'returns hstore values as strings by default' do
246+
@connection.execute "INSERT INTO hstores (tags) VALUES ('1=>2,2=>3')"
247+
x = Hstore.first
248+
assert_instance_of String, x.tags
249+
assert_equal '"1"=>"2", "2"=>"3"', x.tags
250+
end
251+
252+
end unless Test::Unit::TestCase.ar_version('4.0')

0 commit comments

Comments
 (0)