atomic_json 0.1.0

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.
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.16.0.pre.2
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'.freeze
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in atomic_json.gemspec
6
+ gemspec
7
+
8
+ gem 'pg'
data/Gemfile.lock ADDED
@@ -0,0 +1,111 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ atomic_json (0.1.0)
5
+ activerecord (>= 5.0)
6
+ activesupport (~> 5.0)
7
+ pg (~> 0.18, >= 0.18.1)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actionpack (5.1.4)
13
+ actionview (= 5.1.4)
14
+ activesupport (= 5.1.4)
15
+ rack (~> 2.0)
16
+ rack-test (>= 0.6.3)
17
+ rails-dom-testing (~> 2.0)
18
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
19
+ actionview (5.1.4)
20
+ activesupport (= 5.1.4)
21
+ builder (~> 3.1)
22
+ erubi (~> 1.4)
23
+ rails-dom-testing (~> 2.0)
24
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
25
+ activemodel (5.1.4)
26
+ activesupport (= 5.1.4)
27
+ activerecord (5.1.4)
28
+ activemodel (= 5.1.4)
29
+ activesupport (= 5.1.4)
30
+ arel (~> 8.0)
31
+ activesupport (5.1.4)
32
+ concurrent-ruby (~> 1.0, >= 1.0.2)
33
+ i18n (~> 0.7)
34
+ minitest (~> 5.1)
35
+ tzinfo (~> 1.1)
36
+ arel (8.0.0)
37
+ ast (2.4.0)
38
+ builder (3.2.3)
39
+ byebug (10.0.2)
40
+ concurrent-ruby (1.0.5)
41
+ crass (1.0.4)
42
+ erubi (1.7.1)
43
+ factory_bot (4.10.0)
44
+ activesupport (>= 3.0.0)
45
+ i18n (0.9.1)
46
+ concurrent-ruby (~> 1.0)
47
+ jaro_winkler (1.5.1)
48
+ loofah (2.2.2)
49
+ crass (~> 1.0.2)
50
+ nokogiri (>= 1.5.9)
51
+ method_source (0.9.0)
52
+ mini_portile2 (2.3.0)
53
+ minitest (5.10.3)
54
+ nokogiri (1.8.4)
55
+ mini_portile2 (~> 2.3.0)
56
+ parallel (1.12.1)
57
+ parser (2.5.1.2)
58
+ ast (~> 2.4.0)
59
+ pg (0.21.0)
60
+ powerpack (0.1.2)
61
+ rack (2.0.5)
62
+ rack-test (1.0.0)
63
+ rack (>= 1.0, < 3)
64
+ rails-dom-testing (2.0.3)
65
+ activesupport (>= 4.2.0)
66
+ nokogiri (>= 1.6)
67
+ rails-html-sanitizer (1.0.4)
68
+ loofah (~> 2.2, >= 2.2.2)
69
+ railties (5.1.4)
70
+ actionpack (= 5.1.4)
71
+ activesupport (= 5.1.4)
72
+ method_source
73
+ rake (>= 0.8.7)
74
+ thor (>= 0.18.1, < 2.0)
75
+ rainbow (3.0.0)
76
+ rake (10.4.2)
77
+ rubocop (0.58.1)
78
+ jaro_winkler (~> 1.5.1)
79
+ parallel (~> 1.10)
80
+ parser (>= 2.5, != 2.5.1.1)
81
+ powerpack (~> 0.1)
82
+ rainbow (>= 2.2.2, < 4.0)
83
+ ruby-progressbar (~> 1.7)
84
+ unicode-display_width (~> 1.0, >= 1.0.1)
85
+ ruby-progressbar (1.9.0)
86
+ standalone_migrations (5.2.5)
87
+ activerecord (>= 4.2.7, < 5.3.0)
88
+ railties (>= 4.2.7, < 5.3.0)
89
+ rake (>= 10.0)
90
+ thor (0.20.0)
91
+ thread_safe (0.3.6)
92
+ tzinfo (1.2.4)
93
+ thread_safe (~> 0.1)
94
+ unicode-display_width (1.4.0)
95
+
96
+ PLATFORMS
97
+ ruby
98
+
99
+ DEPENDENCIES
100
+ atomic_json!
101
+ bundler (~> 1.16.a)
102
+ byebug (~> 10.0, >= 10.0.2)
103
+ factory_bot (~> 4.0)
104
+ minitest (~> 5.0)
105
+ pg
106
+ rake (~> 10.0)
107
+ rubocop (~> 0.58.1)
108
+ standalone_migrations (~> 5.2, >= 5.2.5)
109
+
110
+ BUNDLED WITH
111
+ 1.16.0.pre.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Antoine Macia
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # AtomicJson
2
+
3
+ Expose a simple set of methods to allow fast atomic updates of `json`/`jsonb` columns of ActiveRecord models using PostgresQL `jsonb_set` [function](https://www.postgresql.org/docs/9.5/static/functions-json.html)
4
+
5
+ - Support updates of `json` and `jsonb` columns
6
+ - Support update of deeply nested fields
7
+ - Support update of multiple fields at once
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'atomic_json'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ ## Usage
22
+
23
+ To update a `json` or `jsonb` column, simply pass a Hash to the bellow method call,
24
+ using the column name as top level key and a nested hash of the value(s) you're willing to update
25
+
26
+ Only the fields you've specified will be updated
27
+
28
+ ```
29
+ order.data
30
+ => { amount: 50.00, first_name: 'Milkpie', last_name: 'Starlord' }
31
+
32
+ order.jsonb_update(data: { amount: 10.00 })
33
+
34
+ order.data
35
+ => { amount: 10.00, first_name: 'Milkpie', last_name: 'Starlord' }
36
+ ```
37
+
38
+ For the sake of simplicity, AtomicJson mimic the behavior of standard ActiveRecord query methods to update database fields
39
+
40
+ ### jsonb_update_columns
41
+
42
+ Same as ActiveRecord `update_columns`, this method will make a straight database update
43
+ - Validations are skipped
44
+ - Callbacks are skipped
45
+ - `updated_at` is not updated
46
+
47
+ ```
48
+ order.jsonb_update_columns(data: { paid: false })
49
+ => true
50
+ ```
51
+
52
+ ### jsonb_update
53
+
54
+ Same as ActiveRecord `update`, this method will
55
+ - Invoke validations
56
+ - Invoke callbacks
57
+ - Touch record `updated_at`
58
+
59
+ ```
60
+ order.jsonb_update(data: { paid: false, product_id: 3772389212 })
61
+ => false
62
+ ```
63
+
64
+ ### jsonb_update!
65
+
66
+ Same as the above `json_update!`, but will raise an `ActiveRecord::RecordInvalid` exception
67
+ if a custom validation fails
68
+
69
+ ```
70
+ order.jsonb_update!(data: { paid: false, product_id: 3772389212 })
71
+ => ActiveRecord::RecordInvalid Exception: Validation failed: data product_id can't be changed
72
+ ```
73
+ ## Todo's
74
+
75
+ - Support update of `json`/`jsonb` arrays via index and key/value id
76
+
77
+ ## Requierements
78
+
79
+ - PostgresQL version 9.5 minimum
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'standalone_migrations'
4
+
5
+ StandaloneMigrations::Tasks.load_tasks
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ Rake::TestTask.new(:benchmark) do |t|
13
+ t.libs << 'test'
14
+ t.test_files = FileList['test/**/*_benchmark.rb']
15
+ end
16
+
17
+ task default: :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'atomic_json/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'atomic_json'
9
+ spec.version = AtomicJson::VERSION
10
+ spec.authors = ['Antoine Macia']
11
+ spec.email = ['antoine@discolabs.com']
12
+
13
+ spec.summary = 'Atomic update of JSON/JSONB fields for ActiveRecord models'
14
+ spec.description = ''
15
+ spec.homepage = ''
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'activerecord', '>= 5.0'
26
+ spec.add_runtime_dependency 'activesupport', '~> 5.0'
27
+ spec.add_runtime_dependency 'pg', '~> 0.18', '>= 0.18.1'
28
+ spec.add_development_dependency 'bundler', '~> 1.16'
29
+ spec.add_development_dependency 'byebug', '~> 10.0', '>= 10.0.2'
30
+ spec.add_development_dependency 'factory_bot', '~> 4.0'
31
+ spec.add_development_dependency 'minitest', '~> 5.0'
32
+ spec.add_development_dependency 'rake', '~> 10.0'
33
+ spec.add_development_dependency 'rubocop', '~> 0.58.1'
34
+ spec.add_development_dependency 'standalone_migrations', '~> 5.2', '>= 5.2.5'
35
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'atomic_json'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/db/config.yml ADDED
@@ -0,0 +1,8 @@
1
+ default: &default
2
+ adapter: postgresql
3
+ encoding: unicode
4
+ pool: 5
5
+
6
+ test:
7
+ <<: *default
8
+ database: atomic_json
@@ -0,0 +1,11 @@
1
+ class CreateMockTestTable < ActiveRecord::Migration[5.1]
2
+
3
+ def change
4
+ create_table :orders do |t|
5
+ t.jsonb :jsonb_data
6
+ t.json :json_data
7
+ t.timestamps
8
+ end
9
+ end
10
+
11
+ end
data/db/schema.rb ADDED
@@ -0,0 +1,25 @@
1
+ # This file is auto-generated from the current state of the database. Instead
2
+ # of editing this file, please use the migrations feature of Active Record to
3
+ # incrementally modify your database, and then regenerate this schema definition.
4
+ #
5
+ # Note that this schema.rb definition is the authoritative source for your
6
+ # database schema. If you need to create the application database on another
7
+ # system, you should be using db:schema:load, not running all the migrations
8
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
9
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
10
+ #
11
+ # It's strongly recommended that you check this file into your version control system.
12
+
13
+ ActiveRecord::Schema.define(version: 20180715062407) do
14
+
15
+ # These are extensions that must be enabled in order to support this database
16
+ enable_extension "plpgsql"
17
+
18
+ create_table "orders", force: :cascade do |t|
19
+ t.jsonb "jsonb_data"
20
+ t.json "json_data"
21
+ t.datetime "created_at", null: false
22
+ t.datetime "updated_at", null: false
23
+ end
24
+
25
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'active_record/connection_adapters/postgresql_adapter'
5
+ require 'atomic_json/query_methods'
6
+
7
+ ActiveSupport.on_load(:active_record) do
8
+ ActiveRecord::Base.public_send(:include, AtomicJson::QueryMethods)
9
+ end
@@ -0,0 +1,28 @@
1
+ module AtomicJson
2
+
3
+ ##
4
+ # Base error class
5
+ class Error < StandardError
6
+ end
7
+
8
+ ##
9
+ # Thrown when attributes
10
+ class TypeError < Error
11
+ end
12
+
13
+ ##
14
+ # Thrown when top level keys provided does not correspond to a JSON/JSONB column
15
+ # on the updated ActiveRecord model
16
+ class InvalidColumnTypeError < Error
17
+ end
18
+
19
+ ##
20
+ # Thrown when the attributes to update are flagged as read-only
21
+ class ReadOnlyAttributeError < Error
22
+ end
23
+
24
+ ##
25
+ # Thrown on specific ActiveRecord errors
26
+ class ActiveRecordError < Error
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AtomicJson
4
+ module JsonQueryHelpers
5
+
6
+ def jsonb_quote_keys(keys)
7
+ "'{#{keys.map(&:to_s).join(',')}}'"
8
+ end
9
+
10
+ def jsonb_quote_value(value)
11
+ %('#{value.to_json}')
12
+ end
13
+
14
+ def concatenation(target, keys, value)
15
+ "#{target}->#{keys.map { |x| quote(x) }.join('->')} || #{jsonb_quote_value(value)}"
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'atomic_json/validations'
4
+ require 'atomic_json/query_builder'
5
+
6
+ module AtomicJson
7
+ class Query
8
+
9
+ include Validations
10
+
11
+ attr_reader :record, :connection, :query_builder
12
+ attr_accessor :query_string
13
+
14
+ delegate :quote_column_name, :quote_table_name, :quote, to: :connection
15
+
16
+ def initialize(record)
17
+ validate_record!(record)
18
+ @record = record
19
+ @connection = ActiveRecord::Base.connection
20
+ @query_builder = QueryBuilder.new(@record, @connection)
21
+ end
22
+
23
+ def build(attributes, touch: false)
24
+ validate_attributes!(record, attributes)
25
+ self.query_string = query_builder.build(attributes, touch)
26
+ self
27
+ end
28
+
29
+ def execute!
30
+ connection.exec_update(query_string, 'SQL')
31
+ rescue ActiveRecord::StatementInvalid => e
32
+ raise Error, e.message
33
+ end
34
+
35
+ def to_s
36
+ query_string
37
+ end
38
+
39
+ end
40
+ end