Add support for Postgresql JSONB #16220

Merged
merged 1 commit into from Jul 25, 2014

Conversation

Projects
None yet
9 participants
@pcreux
Contributor

pcreux commented Jul 18, 2014

This is the updated version of #15195 and #15950 by @pcreux and @chris-teague.

It is tested against postgresql-9.4beta.

Test cases shared between json and jsonb are extracted to a module called PostgresqlJSONSharedTestCases. I made my best to minimise the amount of lines impacted by this extraction.

@chancancode

This comment has been minimized.

Show comment
Hide comment
Member

chancancode commented Jul 18, 2014

👍

@chancancode chancancode added this to the 4.2.0 milestone Jul 18, 2014

activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
+* Add support for Postgresql JSONB
+
+ Philippe Creux, Chris Teague

This comment has been minimized.

@arthurnn

arthurnn Jul 18, 2014

Member

*Philippe Creux*, *Chris Teague*

@arthurnn

arthurnn Jul 18, 2014

Member

*Philippe Creux*, *Chris Teague*

@chancancode

This comment has been minimized.

Show comment
Hide comment
@chancancode

chancancode Jul 18, 2014

Member

cc @senny, @matthewd, @sgrif This is the updated version of #15950, final review? 😄

Member

chancancode commented Jul 18, 2014

cc @senny, @matthewd, @sgrif This is the updated version of #15950, final review? 😄

@chancancode chancancode assigned senny and unassigned matthewd Jul 18, 2014

@@ -29,6 +29,22 @@ def accessor
ActiveRecord::Store::StringKeyedHashAccessor
end
end
+
+ class Jsonb < Json # :nodoc:

This comment has been minimized.

@sgrif

sgrif Jul 18, 2014

Member

Can we put this in its own file?

@sgrif

sgrif Jul 18, 2014

Member

Can we put this in its own file?

This comment has been minimized.

@pcreux

pcreux Jul 18, 2014

Contributor

Sure!

@pcreux

pcreux Jul 18, 2014

Contributor

Sure!

@sgrif

This comment has been minimized.

Show comment
Hide comment
@sgrif

sgrif Jul 18, 2014

Member

One minor comment, looks fine otherwise.

Member

sgrif commented Jul 18, 2014

One minor comment, looks fine otherwise.

@pcreux

This comment has been minimized.

Show comment
Hide comment
@pcreux

pcreux Jul 18, 2014

Contributor

@sgrif I've just added /oid/jsonb.rb. The tests are passing on postgresql-9.4beta.

Contributor

pcreux commented Jul 18, 2014

@sgrif I've just added /oid/jsonb.rb. The tests are passing on postgresql-9.4beta.

+ # the comparison here. Therefore, we need to parse and re-dump the
+ # raw value here to ensure the insignificant whitespaces are
+ # consitent with our encoder's output.
+ raw_old_value = type_cast_for_database(type_cast_from_database(raw_old_value))

This comment has been minimized.

@egilburg

egilburg Jul 18, 2014

Contributor

is this local variable change here recognized in super? does super share lexical scope with the subclass invoking it?

@egilburg

egilburg Jul 18, 2014

Contributor

is this local variable change here recognized in super? does super share lexical scope with the subclass invoking it?

This comment has been minimized.

@matthewd

matthewd Jul 18, 2014

Member

Yes. Though IMO it's better to go with a more obviously-correct super(raw_old_value, new_value) in situations like this.

@matthewd

matthewd Jul 18, 2014

Member

Yes. Though IMO it's better to go with a more obviously-correct super(raw_old_value, new_value) in situations like this.

This comment has been minimized.

@pcreux

pcreux Jul 18, 2014

Contributor

Done!

@pcreux

pcreux Jul 18, 2014

Contributor

Done!

+ begin
+ @connection.transaction do
+ @connection.create_table('json_data_type') do |t|
+ t.json 'payload', :default => {}

This comment has been minimized.

@senny

senny Jul 21, 2014

Member

let's use Ruby 1.9 Hash syntax.

@senny

senny Jul 21, 2014

Member

let's use Ruby 1.9 Hash syntax.

+ begin
+ @connection.transaction do
+ @connection.create_table('json_data_type') do |t|
+ t.jsonb 'payload', :default => {}

This comment has been minimized.

@senny

senny Jul 21, 2014

Member

let's use Ruby 1.9 Hash syntax.

@senny

senny Jul 21, 2014

Member

let's use Ruby 1.9 Hash syntax.

+ def test_column
+ column = JsonDataType.columns_hash["payload"]
+ assert_equal :json, column.type
+ assert_equal "json", column.sql_type

This comment has been minimized.

@senny

senny Jul 21, 2014

Member

we can extract @column_type to a setup method of the subclasses. This way we can also generalize this test.

@senny

senny Jul 21, 2014

Member

we can extract @column_type to a setup method of the subclasses. This way we can also generalize this test.

+ end
+
+ def test_default
+ @connection.add_column 'json_data_type', 'permissions', :json, default: '{"users": "read", "posts": ["read", "write"]}'

This comment has been minimized.

@senny

senny Jul 21, 2014

Member

If we have @column_type we should be able to generalize this test. as well.

@senny

senny Jul 21, 2014

Member

If we have @column_type we should be able to generalize this test. as well.

+ end
+ JsonDataType.reset_column_information
+ column = JsonDataType.columns_hash['users']
+ assert_equal :json, column.type

This comment has been minimized.

@senny

senny Jul 21, 2014

Member

If we have @column_type we should be able to generalize this test. as well.

@senny

senny Jul 21, 2014

Member

If we have @column_type we should be able to generalize this test. as well.

@senny

This comment has been minimized.

Show comment
Hide comment
@senny

senny Jul 21, 2014

Member

@pcreux looking good. Added a few comments then we should be good to go.

Member

senny commented Jul 21, 2014

@pcreux looking good. Added a few comments then we should be good to go.

+class PostgresqlJSONTest < ActiveRecord::TestCase
+ include PostgresqlJSONSharedTestCases
+
+ def column_type

This comment has been minimized.

@pcreux

pcreux Jul 22, 2014

Contributor

@senny I prefer defining the method column_type over this:

def setup
  @column_type = :json
  super
end

I'm happy to use an instance variable instead if you prefer.

@pcreux

pcreux Jul 22, 2014

Contributor

@senny I prefer defining the method column_type over this:

def setup
  @column_type = :json
  super
end

I'm happy to use an instance variable instead if you prefer.

This comment has been minimized.

@senny

senny Jul 23, 2014

Member

method is fine.

@senny

senny Jul 23, 2014

Member

method is fine.

@pcreux

This comment has been minimized.

Show comment
Hide comment
@pcreux

pcreux Jul 24, 2014

Contributor

@senny Rebased against master. Test are passing. It's ready for prime time!

Contributor

pcreux commented Jul 24, 2014

@senny Rebased against master. Test are passing. It's ready for prime time!

@chris-teague

This comment has been minimized.

Show comment
Hide comment

👍

Add support for Postgresql JSONB
[Philippe Creux, Chris Teague]

arunagw pushed a commit to arunagw/rails that referenced this pull request Jul 25, 2014

@senny senny merged commit 99b82fd into rails:master Jul 25, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details

@senny senny referenced this pull request Jul 25, 2014

Closed

PostgreSQL jsonb support #15195

@senny

This comment has been minimized.

Show comment
Hide comment
@senny

senny Jul 25, 2014

Member

thanks guys 💛

Member

senny commented Jul 25, 2014

thanks guys 💛

@sdogruyol

This comment has been minimized.

Show comment
Hide comment
@sdogruyol

sdogruyol Dec 19, 2014

Thanks for jsonb support 💙

Thanks for jsonb support 💙

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment