activerecord-bulk_insert 0.0.1
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 +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +57 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +38 -0
- data/Rakefile +6 -0
- data/activerecord-bulk_insert.gemspec +36 -0
- data/bin/console +10 -0
- data/bin/setup +8 -0
- data/lib/active_record/bulk_insert/version.rb +5 -0
- data/lib/active_record/bulk_insert.rb +60 -0
- data/lib/activerecord-bulk_insert.rb +1 -0
- metadata +158 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7549b76b706ba981379ca0108080c10cb2015f4888317c2906a336bfe16170d6
|
4
|
+
data.tar.gz: 4dcc348245d7a65677aee685683db5ff3f10c3b83de78ec0886ccccae4052d00
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 961c56b939cb3c7207fd1617b50b386340a015df2c3eeb65620409ca3c95c7aa9325a16e52644b8fdd202b278c7b05410d01ed1e1787b96606a078ecadfd2a2b
|
7
|
+
data.tar.gz: d0b6b76994579ffaa2ff8072f832883962a0ecd6f9324d65dfd470124f10e0569e00d09b0fb1eddc2213b7097caa7c4c3e1dfba7ece4c65f8ae82ba831dabdf0
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
|
4
|
+
Layout/IndentationConsistency:
|
5
|
+
EnforcedStyle: indented_internal_methods
|
6
|
+
|
7
|
+
Metrics/AbcSize:
|
8
|
+
Max: 20
|
9
|
+
Exclude:
|
10
|
+
- "lib/seed_gimmick/seed/difference.rb" # FIXME: AbcSize
|
11
|
+
|
12
|
+
Metrics/CyclomaticComplexity:
|
13
|
+
Max: 10
|
14
|
+
|
15
|
+
Metrics/LineLength:
|
16
|
+
Max: 120
|
17
|
+
|
18
|
+
Metrics/MethodLength:
|
19
|
+
Max: 20
|
20
|
+
Exclude:
|
21
|
+
- "lib/seed_gimmick/seed/difference.rb" # FIXME: MethodLength
|
22
|
+
|
23
|
+
Style/BlockDelimiters:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Style/Documentation:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Style/EmptyElse:
|
30
|
+
EnforcedStyle: empty
|
31
|
+
|
32
|
+
Style/FormatString:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
Style/FormatStringToken:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
Style/FrozenStringLiteralComment:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
Style/HashSyntax:
|
42
|
+
Exclude:
|
43
|
+
- "Rakefile"
|
44
|
+
- "**/*.rake"
|
45
|
+
|
46
|
+
Style/MultilineBlockChain:
|
47
|
+
Enabled: false
|
48
|
+
|
49
|
+
Style/NumericLiterals:
|
50
|
+
MinDigits: 7
|
51
|
+
Strict: true
|
52
|
+
|
53
|
+
Style/PercentLiteralDelimiters:
|
54
|
+
Enabled: false
|
55
|
+
|
56
|
+
Style/StringLiterals:
|
57
|
+
EnforcedStyle: double_quotes
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
File without changes
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 Ken Iiboshi
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Activerecord::BulkInsert
|
2
|
+
|
3
|
+
`activerecord-bulk_insert` is provide bulk update and insert for ActiveRecord.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem "activerecord-bulk_insert"
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
users = [*1..10].map { |i| User.new(name: "user#{i}") }
|
21
|
+
User.bulk_insert(users, ignore_new_record: false)
|
22
|
+
|
23
|
+
users = User.all
|
24
|
+
users.each do |user|
|
25
|
+
user.name = "user#{user.id * 2}"
|
26
|
+
end
|
27
|
+
User.bulk_insert(users)
|
28
|
+
```
|
29
|
+
|
30
|
+
## Development
|
31
|
+
|
32
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
33
|
+
|
34
|
+
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).
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/i2bskn/activerecord-bulk_insert.
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "active_record/bulk_insert/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "activerecord-bulk_insert"
|
7
|
+
spec.version = Activerecord::BulkInsert::VERSION
|
8
|
+
spec.authors = ["i2bskn"]
|
9
|
+
spec.email = ["i2bskn@craftake.com"]
|
10
|
+
|
11
|
+
spec.summary = "Bulk insert or update"
|
12
|
+
spec.description = "Bulk insert or update"
|
13
|
+
spec.homepage = "https://github.com/i2bskn/activerecord-bulk_insert"
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = "https://github.com/i2bskn/activerecord-bulk_insert"
|
17
|
+
spec.metadata["changelog_uri"] = "https://github.com/i2bskn/activerecord-bulk_insert/blob/master/CHANGELOG.md"
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_dependency "activerecord", "~> 6.0"
|
29
|
+
spec.add_dependency "activesupport", "~> 6.0"
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
32
|
+
spec.add_development_dependency "pry", "~> 0.12"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
35
|
+
spec.add_development_dependency "rubocop", "~> 0.74"
|
36
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "activerecord-bulk_insert"
|
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
|
+
require "pry"
|
10
|
+
Pry.start
|
data/bin/setup
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require "active_record"
|
2
|
+
require "active_support/concern"
|
3
|
+
require "active_support/lazy_load_hooks"
|
4
|
+
|
5
|
+
require "active_record/bulk_insert/version"
|
6
|
+
|
7
|
+
module ActiveRecord
|
8
|
+
module BulkInsert
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
class_methods do
|
12
|
+
def bulk_insert(objects, ignore_new_record: true)
|
13
|
+
result = objects.group_by(&:new_record?).map { |is_new_record, records|
|
14
|
+
if is_new_record
|
15
|
+
if ignore_new_record
|
16
|
+
records.each(&:save!)
|
17
|
+
else
|
18
|
+
insert_all!(build_attributes(records))
|
19
|
+
end
|
20
|
+
else
|
21
|
+
upsert_all(build_attributes(records))
|
22
|
+
end
|
23
|
+
|
24
|
+
records
|
25
|
+
}
|
26
|
+
|
27
|
+
result.flatten
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def build_attributes(objects)
|
33
|
+
keys = [required_keys, changed_keys(objects)].flatten.uniq
|
34
|
+
|
35
|
+
objects.map { |object| Hash[keys.map { |k| [k, object.public_send(k)] }] }
|
36
|
+
end
|
37
|
+
|
38
|
+
def required_keys
|
39
|
+
@required_keys ||= columns.map { |column|
|
40
|
+
next if column.null
|
41
|
+
|
42
|
+
column.name
|
43
|
+
}.compact - [primary_key]
|
44
|
+
end
|
45
|
+
|
46
|
+
def changed_keys(objects)
|
47
|
+
now = Time.current
|
48
|
+
objects.map { |object|
|
49
|
+
object.updated_at = now
|
50
|
+
object.created_at ||= now
|
51
|
+
object.changed_attributes.keys
|
52
|
+
}.flatten.uniq.compact
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
ActiveSupport.on_load(:active_record) do
|
59
|
+
ActiveRecord::Base.include(ActiveRecord::BulkInsert)
|
60
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "active_record/bulk_insert"
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-bulk_insert
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- i2bskn
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-08-23 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: '6.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '6.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '6.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.12'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.12'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.74'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.74'
|
111
|
+
description: Bulk insert or update
|
112
|
+
email:
|
113
|
+
- i2bskn@craftake.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".rubocop.yml"
|
121
|
+
- ".travis.yml"
|
122
|
+
- CHANGELOG.md
|
123
|
+
- Gemfile
|
124
|
+
- LICENSE
|
125
|
+
- README.md
|
126
|
+
- Rakefile
|
127
|
+
- activerecord-bulk_insert.gemspec
|
128
|
+
- bin/console
|
129
|
+
- bin/setup
|
130
|
+
- lib/active_record/bulk_insert.rb
|
131
|
+
- lib/active_record/bulk_insert/version.rb
|
132
|
+
- lib/activerecord-bulk_insert.rb
|
133
|
+
homepage: https://github.com/i2bskn/activerecord-bulk_insert
|
134
|
+
licenses: []
|
135
|
+
metadata:
|
136
|
+
homepage_uri: https://github.com/i2bskn/activerecord-bulk_insert
|
137
|
+
source_code_uri: https://github.com/i2bskn/activerecord-bulk_insert
|
138
|
+
changelog_uri: https://github.com/i2bskn/activerecord-bulk_insert/blob/master/CHANGELOG.md
|
139
|
+
post_install_message:
|
140
|
+
rdoc_options: []
|
141
|
+
require_paths:
|
142
|
+
- lib
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
requirements: []
|
154
|
+
rubygems_version: 3.0.3
|
155
|
+
signing_key:
|
156
|
+
specification_version: 4
|
157
|
+
summary: Bulk insert or update
|
158
|
+
test_files: []
|