activerecord-postgres-json 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 407ecfb24032a8c34bce6bc34eb02e6ce4845b13ea1d663b41476d8cb59fa3f4
4
- data.tar.gz: 1c8e108f8126b9831b20d8f78f2ccac0d00857b4f3c573345d6ebacaa727b43c
3
+ metadata.gz: 0e25b4dcf74b8f989c6a5bfe4b54e48ee733e5def17452dfca59d61c756b3db2
4
+ data.tar.gz: 0a9eae8d07eaf12f97688f50fa19952d3d76082148a3d55f587157176c25247c
5
5
  SHA512:
6
- metadata.gz: f497075d5d89ff8b487d5dd13246cf11ec222d4b681f67b8a48c136f5b3cce4f1fe0edb2bbafe4b6be31630db8e34eca19ad56be72e80eca0415af8542a5329e
7
- data.tar.gz: 8061bde4477a992fc2dbac854c2c378f88278eba07c97b531b76f36f3acc70bccd89f75449105a95efbcbceca882a555ffdec4a7eb14178a00674afcb9d36df0
6
+ metadata.gz: a6c3a987892882a1f4887ffa39bcf502845efc4863cb461cf047c8d829ca7a4da1b3f00f5c51e6274c31720cbab75b56efde55efa6a7d9be97690c4a70900988
7
+ data.tar.gz: c6571acbf93ea8647e7acaeb95dd9a81bf3130fb16e9468a4c8d11069f49558c9e2e018bf44304184b971aa95e77bc444ec1b49d09bd5bfe46d7a810db6bf7d8
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: activerecord-postgres-json 0.2.2 ruby lib
5
+ # stub: activerecord-postgres-json 0.2.3 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "activerecord-postgres-json".freeze
9
- s.version = "0.2.2"
9
+ s.version = "0.2.3"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["Roman Shterenzon".freeze]
14
- s.date = "2020-09-14"
14
+ s.date = "2020-09-15"
15
15
  s.description = "Active Record support for PostgreSQL JSON type".freeze
16
16
  s.email = "romanbsd@yahoo.com".freeze
17
17
  s.extra_rdoc_files = [
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "Rakefile",
29
29
  "VERSION",
30
30
  "activerecord-postgres-json.gemspec",
31
+ "db/ar_postgre_json_test.sql",
31
32
  "lib/activerecord-postgres-json.rb",
32
33
  "lib/activerecord-postgres-json/activerecord.rb",
33
34
  "lib/activerecord-postgres-json/coders.rb",
@@ -0,0 +1,43 @@
1
+ SET statement_timeout = 0;
2
+ SET lock_timeout = 0;
3
+ SET idle_in_transaction_session_timeout = 0;
4
+ SET client_encoding = 'UTF8';
5
+ SET standard_conforming_strings = on;
6
+ SELECT pg_catalog.set_config('search_path', '', false);
7
+ SET check_function_bodies = false;
8
+ SET xmloption = content;
9
+ SET client_min_messages = warning;
10
+ SET row_security = off;
11
+
12
+ SET default_tablespace = '';
13
+
14
+ CREATE TABLE public.posts (
15
+ id integer NOT NULL,
16
+ old_data json,
17
+ data jsonb
18
+ );
19
+
20
+ CREATE INDEX index_posts_data_gin ON public.posts USING gin (data);
21
+
22
+ CREATE VIEW public.post_questions AS
23
+ SELECT posts.id,
24
+ (posts.data ->> 'title'::text) AS title,
25
+ (posts.data #>> '{author,name}'::text[]) AS author_name,
26
+ (posts.data #>> '{author,email}'::text[]) AS author_email,
27
+ ((posts.data ->> 'tags'::text))::jsonb AS tags,
28
+ ((posts.data ->> 'draft'::text))::boolean AS draft
29
+ FROM public.posts;
30
+
31
+ CREATE SEQUENCE public.posts_id_seq
32
+ AS integer
33
+ START WITH 1
34
+ INCREMENT BY 1
35
+ NO MINVALUE
36
+ NO MAXVALUE
37
+ CACHE 1;
38
+
39
+ ALTER SEQUENCE public.posts_id_seq OWNED BY public.posts.id;
40
+
41
+ ALTER TABLE ONLY public.posts ALTER COLUMN id SET DEFAULT nextval('public.posts_id_seq'::regclass);
42
+
43
+ ALTER TABLE ONLY public.posts ADD CONSTRAINT posts_pkey PRIMARY KEY (id);
@@ -5,15 +5,20 @@ require 'active_record/connection_adapters/postgresql_adapter'
5
5
 
6
6
  module ActiveRecord
7
7
  module ConnectionAdapters
8
- PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:json] = { name: 'json' }
8
+ # jsonb type isn't known by Rails 3.2.x, 4.0.x and 4.1.x, so the gem is defining it
9
9
  PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:jsonb] = { name: 'jsonb' }
10
10
 
11
- if ::ActiveRecord.version >= Gem::Version.new('4.0.0')
12
- # As suggested here: http://www.innovationontherun.com/fixing-unknown-oid-geography-errors-with-postgis-and-rails-4-0/
13
- # to prevent the Rails 4.0/4.1 error of
14
- # "unknown OID: {field}"
15
- PostgreSQLAdapter::OID.register_type('json', PostgreSQLAdapter::OID::Identity.new)
16
- PostgreSQLAdapter::OID.register_type('jsonb', PostgreSQLAdapter::OID::Identity.new)
11
+ if ActiveRecord.respond_to?(:version)
12
+ if ::ActiveRecord.version >= Gem::Version.new('4.0.0') && ::ActiveRecord.version < Gem::Version.new('4.2.0')
13
+ # As suggested here:
14
+ # http://www.innovationontherun.com/fixing-unknown-oid-geography-errors-with-postgis-and-rails-4-0/
15
+ # to prevent the Rails 4.0/4.1 error of "unknown OID: {field}"
16
+ PostgreSQLAdapter::OID.register_type('jsonb', PostgreSQLAdapter::OID::Identity.new)
17
+ end
18
+ elsif ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 2
19
+ # json type isn't known by Rails 3.2.x, so the gem is defining it.
20
+ # Rails 4.0.x and 4.1.x already have it defined
21
+ PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:json] = { name: 'json' }
17
22
  end
18
23
 
19
24
  class PostgreSQLColumn < Column
@@ -3,6 +3,9 @@ require File.expand_path(File.dirname(__FILE__) + '/support/database_setup')
3
3
 
4
4
  class Post < ActiveRecord::Base
5
5
  serialize :data, ActiveRecord::Coders::JSON.new(symbolize_keys: true)
6
+
7
+ # For Rails 3.2.x the json serializer should be explicitly specified, because it is unknown by Rails
8
+ serialize :old_data, ActiveRecord::Coders::JSON.new(symbolize_keys: true) unless ActiveRecord.respond_to?(:version)
6
9
  end
7
10
 
8
11
  class PostQuestions < ActiveRecord::Base
@@ -10,36 +13,11 @@ class PostQuestions < ActiveRecord::Base
10
13
  end
11
14
 
12
15
  describe 'ActiverecordPostgresJson', db: true do
13
- before(:all) do
14
- ActiveRecord::Schema.define do
15
- create_table :posts, force: true do |t|
16
- t.column :data, :jsonb
17
- end
18
- end
19
-
20
- Post.reset_column_information
21
-
22
- ActiveRecord::Base.connection.execute <<-SQL
23
- CREATE INDEX index_posts_data_gin ON posts
24
- USING gin (data);
25
- SQL
26
-
27
- ActiveRecord::Base.connection.execute <<-SQL
28
- CREATE OR REPLACE VIEW post_questions as
29
- SELECT
30
- id,
31
- data ->> 'title' AS title,
32
- data #>> '{author,name}' AS author_name,
33
- data #>> '{author,email}' AS author_email,
34
- (data ->> 'tags')::jsonb AS tags,
35
- (data ->> 'draft')::boolean AS draft
36
- FROM posts
37
- SQL
38
- end
39
-
40
16
  after(:all) do
41
- ActiveRecord::Base.connection.execute 'DROP INDEX IF EXISTS index_posts_data_gin'
42
- ActiveRecord::Base.connection.execute 'DROP VIEW IF EXISTS post_questions'
17
+ db_config = YAML.load_file(File.expand_path('../database.yml', __FILE__))
18
+ ActiveRecord::Base.connection.execute "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'mydb'"
19
+ ActiveRecord::Base.establish_connection(db_config['test'].merge('database' => 'postgres', 'schema_search_path' => 'public'))
20
+ ActiveRecord::Base.connection.execute 'DROP DATABASE IF EXISTS ar_postgres_json_test'
43
21
  end
44
22
 
45
23
  before { Post.delete_all }
@@ -100,4 +78,31 @@ describe 'ActiverecordPostgresJson', db: true do
100
78
  it 'when search in objects array' do
101
79
  expect(Post.where('data @> \'[{"f1":6}]\'').count).to eq(1)
102
80
  end
81
+
82
+ context 'when the column is of json type' do
83
+ let!(:plain_json) do
84
+ Post.create!(data: { title: 'Plain JSON support'},
85
+ old_data: { title: 'Old JSON is supported too',
86
+ author: { name: 'Aurel', email: 'aurel@example.com'},
87
+ draft: true }).reload
88
+ end
89
+
90
+ it 'process plain JSON columns, ' do
91
+ post = Post.all.to_a[4]
92
+
93
+ # Rails 4.0.x and 4.1.x will serialize plain json fields without symbolizing keys with their own serializer
94
+ if ActiveRecord.respond_to?(:version)
95
+ if ::ActiveRecord.version >= Gem::Version.new('4.0.0') && ::ActiveRecord.version < Gem::Version.new('4.2.0')
96
+ expect(post.old_data).to eq({ 'title' => 'Old JSON is supported too',
97
+ 'author' => { 'name' => 'Aurel', 'email' => 'aurel@example.com'},
98
+ 'draft' => true })
99
+ end
100
+ # Rails 3.2.x will serialize plain json fields symbolizing keys using this gem
101
+ elsif ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 2
102
+ expect(post.old_data).to eq({ title: 'Old JSON is supported too',
103
+ author: { name: 'Aurel', email: 'aurel@example.com'},
104
+ draft: true })
105
+ end
106
+ end
107
+ end
103
108
  end
@@ -3,16 +3,37 @@ require 'pg'
3
3
  require 'yaml'
4
4
 
5
5
  db_config = YAML.load_file(File.expand_path('../../database.yml', __FILE__))
6
+ structure_sql_filename = 'db/ar_postgre_json_test.sql'
6
7
 
7
8
  begin
8
9
  ActiveRecord::Base.establish_connection db_config['test']
9
10
  ActiveRecord::Base.connection.active?
10
- rescue Exception => e
11
+ rescue Exception => _e
11
12
  encoding = db_config['test']['encoding'] || ENV['CHARSET'] || 'utf8'
12
13
  begin
13
14
  ActiveRecord::Base.establish_connection(db_config['test'].merge('database' => 'postgres', 'schema_search_path' => 'public'))
14
- ActiveRecord::Base.connection.create_database(db_config['test']['database'], db_config['test'].merge('encoding' => encoding))
15
+ ActiveRecord::Base.connection.recreate_database(db_config['test']['database'], db_config['test'].merge('encoding' => encoding))
15
16
  ActiveRecord::Base.establish_connection(db_config['test'])
17
+
18
+ ActiveRecord::Base.configurations = db_config
19
+ if ActiveRecord.respond_to?(:version)
20
+ require 'active_record/tasks/database_tasks'
21
+
22
+ if ::ActiveRecord.version >= Gem::Version.new('4.1.0') && ::ActiveRecord.version < Gem::Version.new('4.2.0')
23
+ ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:sql, structure_sql_filename, 'test')
24
+ elsif ::ActiveRecord.version >= Gem::Version.new('4.0.0') && ::ActiveRecord.version < Gem::Version.new('4.1.0')
25
+ ActiveRecord::Tasks::DatabaseTasks.structure_load(db_config['test'], structure_sql_filename)
26
+ end
27
+ elsif ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 2
28
+ require "active_record/base"
29
+ require "rake"
30
+ require "rake/dsl_definition"
31
+ load "active_record/railties/databases.rake"
32
+
33
+ set_psql_env(db_config)
34
+ `psql -f "#{structure_sql_filename}" #{db_config['test']['database']}`
35
+ end
36
+
16
37
  rescue Exception => ec
17
38
  $stderr.puts ec, *(ec.backtrace)
18
39
  $stderr.puts "Couldn't create database for #{db_config['test'].inspect}"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-postgres-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Shterenzon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-14 00:00:00.000000000 Z
11
+ date: 2020-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -145,6 +145,7 @@ files:
145
145
  - Rakefile
146
146
  - VERSION
147
147
  - activerecord-postgres-json.gemspec
148
+ - db/ar_postgre_json_test.sql
148
149
  - lib/activerecord-postgres-json.rb
149
150
  - lib/activerecord-postgres-json/activerecord.rb
150
151
  - lib/activerecord-postgres-json/coders.rb