Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Postgresql JSONB #16220

Merged
merged 1 commit into from Jul 25, 2014
Merged

Conversation

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

chancancode commented Jul 18, 2014

👍

@chancancode chancancode added this to the 4.2.0 milestone Jul 18, 2014
@arthurnn
arthurnn reviewed Jul 18, 2014
View changes
activerecord/CHANGELOG.md Outdated
@@ -1,3 +1,7 @@
* Add support for Postgresql JSONB

Philippe Creux, Chris Teague

This comment has been minimized.

Copy link
@arthurnn

arthurnn Jul 18, 2014

Member

*Philippe Creux*, *Chris Teague*

@chancancode
Copy link
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
@sgrif
sgrif reviewed Jul 18, 2014
View changes
activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb Outdated
@@ -29,6 +29,22 @@ def accessor
ActiveRecord::Store::StringKeyedHashAccessor
end
end

class Jsonb < Json # :nodoc:

This comment has been minimized.

Copy link
@sgrif

sgrif Jul 18, 2014

Contributor

Can we put this in its own file?

This comment has been minimized.

Copy link
@pcreux

pcreux Jul 18, 2014

Author Contributor

Sure!

@sgrif
Copy link
Contributor

sgrif commented Jul 18, 2014

One minor comment, looks fine otherwise.

@pcreux
Copy link
Contributor Author

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.

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

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

Copy link
@pcreux

pcreux Jul 18, 2014

Author Contributor

Done!

@senny
senny reviewed Jul 21, 2014
View changes
activerecord/test/cases/adapters/postgresql/json_test.rb Outdated
begin
@connection.transaction do
@connection.create_table('json_data_type') do |t|
t.json 'payload', :default => {}

This comment has been minimized.

Copy link
@senny

senny Jul 21, 2014

Member

let's use Ruby 1.9 Hash syntax.

@senny
senny reviewed Jul 21, 2014
View changes
activerecord/test/cases/adapters/postgresql/json_test.rb Outdated
begin
@connection.transaction do
@connection.create_table('json_data_type') do |t|
t.jsonb 'payload', :default => {}

This comment has been minimized.

Copy link
@senny

senny Jul 21, 2014

Member

let's use Ruby 1.9 Hash syntax.

@senny
senny reviewed Jul 21, 2014
View changes
activerecord/test/cases/adapters/postgresql/json_test.rb Outdated
def test_column
column = JsonDataType.columns_hash["payload"]
assert_equal :json, column.type
assert_equal "json", column.sql_type

This comment has been minimized.

Copy link
@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 reviewed Jul 21, 2014
View changes
activerecord/test/cases/adapters/postgresql/json_test.rb Outdated
end

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

This comment has been minimized.

Copy link
@senny

senny Jul 21, 2014

Member

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

@senny
senny reviewed Jul 21, 2014
View changes
activerecord/test/cases/adapters/postgresql/json_test.rb Outdated
end
JsonDataType.reset_column_information
column = JsonDataType.columns_hash['users']
assert_equal :json, column.type

This comment has been minimized.

Copy link
@senny

senny Jul 21, 2014

Member

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

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

Copy link
@pcreux

pcreux Jul 22, 2014

Author 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.

Copy link
@senny

senny Jul 23, 2014

Member

method is fine.

@pcreux
Copy link
Contributor Author

pcreux commented Jul 24, 2014

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

@chris-teague
Copy link

chris-teague commented Jul 24, 2014

👍

[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
1 check passed
continuous-integration/travis-ci The Travis CI build passed
Details
@senny senny mentioned this pull request Jul 25, 2014
@senny
Copy link
Member

senny commented Jul 25, 2014

thanks guys 💛

@sdogruyol
Copy link

sdogruyol commented Dec 19, 2014

Thanks for jsonb support 💙

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

9 participants
You can’t perform that action at this time.