activerecord_mass_insert 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b955518970b523bd4db4c1a1ff0f456bbbd2215d
4
+ data.tar.gz: d190a9b6825700bf95fced9b9b55bd372b63eabd
5
+ SHA512:
6
+ metadata.gz: 7cb4a29703ad023518508d7a0b6f7db0811a2d49fc6ad4bb9c99f0f05d2fbda163b2210785b7a610f181903e179782974c7a9a72ae768dced4b9e3e08698b0de
7
+ data.tar.gz: aa6e97573a3635b817082f80ec13e225c7df46c42704dddbbeb120aec2b6bc0e7fabc9fd61a8eef4f93e2e8807f8f4ed176aa6a232687669bd17c54a3765149d
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ .DS_Store
2
+ *.DS_Store
3
+
4
+ Gemfile.lock
5
+ /gemfiles/*.gemfile.lock
6
+
7
+ /.bundle/
8
+ /.yardoc
9
+ /_yardoc/
10
+ /coverage/
11
+ /doc/
12
+ /pkg/
13
+ /spec/reports/
14
+ /tmp/
15
+
16
+ # rspec failure tracking
17
+ .rspec_status
18
+
19
+ *.gem
data/.rubocop.yml ADDED
@@ -0,0 +1,12 @@
1
+ AllCops:
2
+ Exclude:
3
+ - '*.gemspec'
4
+
5
+ Style/MultilineBlockChain:
6
+ Enabled: false
7
+
8
+ Metrics/LineLength:
9
+ Max: 100
10
+
11
+ Metrics/BlockLength:
12
+ ExcludedMethods: ['describe', 'context']
data/Appraisals ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ appraise 'activerecord-4.2' do
4
+ gem 'pg', '~> 0.20.0'
5
+ gem 'activerecord', '~> 4.2.0', require: 'active_record'
6
+ end
7
+
8
+ appraise 'activerecord-5.0' do
9
+ gem 'pg', '~> 0.21'
10
+ gem 'activerecord', '~> 5.0.0', require: 'active_record'
11
+ end
12
+
13
+ appraise 'activerecord-5.1' do
14
+ gem 'pg', '~> 0.21'
15
+ gem 'activerecord', '~> 5.1.0', require: 'active_record'
16
+ end
17
+
18
+ appraise 'activerecord-5.2' do
19
+ gem 'pg'
20
+ gem 'activerecord', '~> 5.2.0', require: 'active_record'
21
+ end
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in activerecord_mass_insert.gemspec
6
+ gemspec
7
+ gem 'activerecord', require: 'active_record'
8
+ gem 'pg'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Andrew Rove
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,52 @@
1
+ ## Installation
2
+
3
+ Requires Postgresql 9.5+
4
+
5
+ ActiveRecord >= 4.2 supported and tested.
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'activerecord_mass_insert'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install activerecord_mass_insert
20
+
21
+ ## Usage
22
+ ```ruby
23
+ payload = '[{"name":"Madison","breed":"Golden","meta":{"rescue":false,"age":null}},{"name":"Daisy","meta":{"rescue":true,"age":18}},{"name":"Gracey","meta":{"rescue":false,"nickname":"Scoogie","age":11}},{"name":"Sadie","meta":{"rescue":true,"dingo_blood":true,"age":11}},{"name":"Raymond","meta":{"rescue":null,"nickname":"Radar","tail":false,"age":11}},{"name":"Nemo","meta":{"rescue":true,"number_of_ears":1,"age":2}}]'
24
+
25
+ dog_ids = Dog.mass_insert(payload, :name, created_at: 'NOW()', updated_at: 'NOW()')
26
+
27
+ dogs = Dog.find(dog_ids)
28
+
29
+ puts dogs.count # => 6
30
+ puts dogs.first.name # => Madison
31
+ ```
32
+
33
+ For a payload you can pass a JSON objects by itself, in a JSON array, or a ruby array. You can also
34
+ pass a ruby object that responds to to_json by itself or in an array. JSON will never be parsed to ruby.
35
+ This done to ensure no time is wasted parsing json twice.
36
+
37
+ ## Development
38
+
39
+ Requires Postgresql 9.5+
40
+
41
+ After checking out the repo, run `gem install bundle`, then `bundle install`, and run `bin/setup` to install dependencies. Then, run `bundle exec appraisal rspec` to run the tests, this includes the lints. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rovermicrover/activerecord_mass_insert.
48
+
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,36 @@
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 'activerecord/mass_insert/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'activerecord_mass_insert'
9
+ spec.version = ActiveRecord::MassInsert::VERSION.join('.')
10
+ spec.date = '2019-05-01'
11
+ spec.summary = 'Mass Insert For ActiveRecord'
12
+ spec.description = 'Mass Insert For ActiveRecord via Postgresql json_to_recordset'
13
+ spec.authors = ['Andrew Rove']
14
+ spec.email = 'andrew.m.rove@gmail.com'
15
+ spec.homepage = 'https://rubygemspec.org/gems/activerecord_mass_insert'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features|gemfiles)/})
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_runtime_dependency 'activerecord', '>= 4.2.0'
26
+ spec.add_runtime_dependency 'pg', '>= 0.20.0'
27
+
28
+ spec.add_development_dependency 'appraisal', '~> 2.2'
29
+ spec.add_development_dependency 'bundler', '~> 1.14'
30
+ spec.add_development_dependency 'pry', '~> 0.12'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'rspec', '~> 3.0'
33
+ spec.add_development_dependency 'rubocop', '~> 0.69'
34
+ spec.add_development_dependency 'simplecov', '~> 0.16'
35
+ spec.add_development_dependency 'brakeman', '~> 4.5'
36
+ end
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ load 'spec/spec_helper.rb'
6
+ require 'activerecord/mass_insert'
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require 'irb'
16
+ 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 exec appraisal install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module MassInsert
5
+ # Helper for adding mass_insert to ActiveRecord::Base
6
+ module Helper
7
+ def mass_insert(payload, *matching_columns, **mapped_columns)
8
+ Inserter.insert(
9
+ self, payload, *matching_columns, **mapped_columns
10
+ ).to_a.map { |result| result['id'] }
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ ActiveRecord::Base.extend(ActiveRecord::MassInsert::Helper)
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module ActiveRecord
6
+ module MassInsert
7
+ # Handles the building of the mass insert statement
8
+ class Inserter < Statement
9
+ extend Forwardable
10
+
11
+ def_delegators :model, :primary_key, :arel_table, :connection
12
+
13
+ def self.insert(*args)
14
+ new(*args).insert
15
+ end
16
+
17
+ def insert
18
+ connection.execute(sql)
19
+ end
20
+
21
+ def sql
22
+ [
23
+ model.send(:sanitize_sql_array, [statement.to_sql, payload]),
24
+ 'RETURNING',
25
+ primary_key
26
+ ].join(' ')
27
+ end
28
+
29
+ def statement
30
+ Arel::Nodes::InsertStatement.new.tap do |new_statement|
31
+ new_statement.relation = arel_table
32
+ new_statement.values = projection.statement
33
+ new_statement.columns = columns
34
+ end
35
+ end
36
+
37
+ def columns
38
+ matching_columns.concat(mapped_columns.keys).map do |column|
39
+ arel_table[column]
40
+ end
41
+ end
42
+
43
+ def projection
44
+ Projection.new(model, payload, *matching_columns, **mapped_columns)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+ require 'securerandom'
5
+
6
+ module ActiveRecord
7
+ module MassInsert
8
+ # Handles the building of the select statement that projects the JSON array
9
+ class Projection < Statement
10
+ extend Forwardable
11
+
12
+ def_delegators :model, :column_for_attribute
13
+
14
+ def statement
15
+ init_args = []
16
+ init_args << arel_table.engine if arel_table.respond_to?(:engine)
17
+
18
+ Arel::SelectManager.new(*init_args).tap do |select_manager|
19
+ select_manager.project(select)
20
+ select_manager.from(table)
21
+ end
22
+ end
23
+
24
+ def select
25
+ [
26
+ *matching_columns.map { |column| arel_table[column] },
27
+ *select_mapped_columns
28
+ ]
29
+ end
30
+
31
+ def select_mapped_columns
32
+ mapped_columns.map do |column, mapping|
33
+ mapping = mapping.call(arel_table, column) if mapping.is_a?(Proc)
34
+ case mapping
35
+ when Arel::Nodes::Node
36
+ mapping
37
+ else
38
+ Arel::Nodes::SqlLiteral.new(mapping.to_s)
39
+ end
40
+ end
41
+ end
42
+
43
+ def column_sql_definitions
44
+ matching_columns.map do |column|
45
+ column_for_attribute(column)
46
+ end.map do |column_objects|
47
+ [column_objects.name, column_objects.sql_type].join(' ')
48
+ end
49
+ end
50
+
51
+ def table_name
52
+ @table_name ||= ['json_projection', SecureRandom.hex].join('_')
53
+ end
54
+
55
+ def arel_table
56
+ @arel_table ||= Arel::Table.new(table_name)
57
+ end
58
+
59
+ def table
60
+ Arel::Nodes::NamedFunction.new(
61
+ 'json_to_recordset', [Arel::Nodes::SqlLiteral.new('?')]
62
+ ).as(
63
+ [table_name, '(', column_sql_definitions.join(', '), ')'].join
64
+ )
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module MassInsert
5
+ # Common structure for all SQL statement builders
6
+ class Statement
7
+ attr_reader :model, :payload, :matching_columns, :mapped_columns
8
+
9
+ def initialize(model, payload, *matching_columns, **mapped_columns)
10
+ @model = model
11
+ @payload = payload_to_json_array(payload)
12
+ @matching_columns = matching_columns
13
+ @mapped_columns = mapped_columns
14
+ end
15
+
16
+ private
17
+
18
+ def payload_to_json_array(new_payload)
19
+ if new_payload.is_a?(Array) && new_payload.all? { |p| p.is_a?(String) }
20
+ # If payload is an array of strings, assume its an array
21
+ # of json objects. contact them togther to form the
22
+ # inner part of a json array.
23
+ new_payload = new_payload.join(',')
24
+ elsif !new_payload.is_a?(String)
25
+ # Otherwise transform to JSON
26
+ new_payload = new_payload.to_json
27
+ end
28
+ # If payload is not a json array wrap it in brackets to make it so.
29
+ new_payload.start_with?('[') ? new_payload : "[#{new_payload}]"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module MassInsert
5
+ VERSION = [0, 0, 1].freeze
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'activerecord/mass_insert/version'
4
+ require 'activerecord/mass_insert/statement'
5
+ require 'activerecord/mass_insert/inserter'
6
+ require 'activerecord/mass_insert/projection'
7
+ require 'activerecord/mass_insert/helper'
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'activerecord/mass_insert'
metadata ADDED
@@ -0,0 +1,199 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_mass_insert
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Rove
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.20.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.20.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: appraisal
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.12'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.69'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.69'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.16'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.16'
139
+ - !ruby/object:Gem::Dependency
140
+ name: brakeman
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '4.5'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '4.5'
153
+ description: Mass Insert For ActiveRecord via Postgresql json_to_recordset
154
+ email: andrew.m.rove@gmail.com
155
+ executables: []
156
+ extensions: []
157
+ extra_rdoc_files: []
158
+ files:
159
+ - ".gitignore"
160
+ - ".rubocop.yml"
161
+ - Appraisals
162
+ - Gemfile
163
+ - LICENSE.txt
164
+ - README.md
165
+ - activerecord_mass_insert.gemspec
166
+ - bin/console
167
+ - bin/setup
168
+ - lib/activerecord/mass_insert.rb
169
+ - lib/activerecord/mass_insert/helper.rb
170
+ - lib/activerecord/mass_insert/inserter.rb
171
+ - lib/activerecord/mass_insert/projection.rb
172
+ - lib/activerecord/mass_insert/statement.rb
173
+ - lib/activerecord/mass_insert/version.rb
174
+ - lib/activerecord_mass_insert.rb
175
+ homepage: https://rubygemspec.org/gems/activerecord_mass_insert
176
+ licenses:
177
+ - MIT
178
+ metadata: {}
179
+ post_install_message:
180
+ rdoc_options: []
181
+ require_paths:
182
+ - lib
183
+ required_ruby_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ required_rubygems_version: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ version: '0'
193
+ requirements: []
194
+ rubyforge_project:
195
+ rubygems_version: 2.6.11
196
+ signing_key:
197
+ specification_version: 4
198
+ summary: Mass Insert For ActiveRecord
199
+ test_files: []