fast_inserter 0.1.6 → 1.0.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.ruby-version +1 -1
- data/.travis.yml +3 -3
- data/CHANGELOG.md +11 -1
- data/README.md +3 -3
- data/fast_inserter.gemspec +1 -1
- data/lib/fast_inserter/fast_inserter_base.rb +35 -13
- data/lib/fast_inserter/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3bbe7d518954dce221c2fecd334c6441f664b9c02041a4c41dc99f231af6801d
|
4
|
+
data.tar.gz: '028e339eb3b132caf9cb397c1c9e13e5505b346654ed6a01146c7e1ec7da5ed0'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 93828bca03374757e898a731bb33ca9f957f391ef06e1e967ae662874ce49d2a4fdfc75feab2557168299cb22c8bd5ce70d480eeaa9e4ffa0d104a97dda24fef
|
7
|
+
data.tar.gz: efd8eb195e7556e5020edd88341a1f56f1c5f036720e5c4b7ca08dbb5579f56e05a56d651753865d806116bd312fd05b917154f7f36d067c4eabbbd216576a75
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.3
|
1
|
+
2.5.3
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,14 @@
|
|
1
|
-
##
|
1
|
+
## 1.0.0.pre (January 7, 2019) ##
|
2
|
+
|
3
|
+
* Use database IN clause when checking for existing records.
|
4
|
+
|
5
|
+
*Jordon Dornbos*
|
6
|
+
|
7
|
+
## 0.1.6 (July 3, 2018) ##
|
8
|
+
|
9
|
+
* Fix check_for_existing with multiple variable columns.
|
10
|
+
|
11
|
+
*Josh Warfield*
|
2
12
|
|
3
13
|
* Remove support for Ruby 2.2. Add support for Ruby 2.4 and 2.5.
|
4
14
|
|
data/README.md
CHANGED
@@ -51,9 +51,9 @@ params = {
|
|
51
51
|
options: {
|
52
52
|
timestamps: true,
|
53
53
|
unique: true,
|
54
|
-
check_for_existing: true
|
55
|
-
group_size: 2_000
|
54
|
+
check_for_existing: true
|
56
55
|
},
|
56
|
+
group_size: 2_000,
|
57
57
|
variable_column: 'user_id',
|
58
58
|
values: user_ids
|
59
59
|
}
|
@@ -89,7 +89,7 @@ Queries the table for any values which already exist and removes them from the v
|
|
89
89
|
|
90
90
|
### group_size
|
91
91
|
|
92
|
-
Insertions will be broken up into batches. This specifies the number of records you want to insert per batch. Default is
|
92
|
+
Insertions will be broken up into batches. This specifies the number of records you want to insert per batch. Default is 1,000.
|
93
93
|
|
94
94
|
### variable_column
|
95
95
|
|
data/fast_inserter.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.add_runtime_dependency 'activerecord', '>= 4.1.0'
|
23
23
|
|
24
|
-
spec.required_ruby_version = ">= 2.
|
24
|
+
spec.required_ruby_version = ">= 2.3.8"
|
25
25
|
|
26
26
|
spec.add_development_dependency "bundler"
|
27
27
|
spec.add_development_dependency "rake", "~> 10.0"
|
@@ -18,8 +18,7 @@
|
|
18
18
|
# options: {
|
19
19
|
# timestamps: true,
|
20
20
|
# unique: true,
|
21
|
-
# check_for_existing: true
|
22
|
-
# group_size: 1_000
|
21
|
+
# check_for_existing: true
|
23
22
|
# },
|
24
23
|
# variable_column: 'user_id',
|
25
24
|
# values: user_ids
|
@@ -39,11 +38,11 @@
|
|
39
38
|
# A hash representing additional column values to set that you don't want
|
40
39
|
# to include in uniqueness checks or other pre-insertion operations.
|
41
40
|
# group_size: Integer
|
42
|
-
# The number of items you want to insert per batch of records.
|
41
|
+
# The number of items you want to insert per batch of records. Defaults to 1_000.
|
43
42
|
#
|
44
43
|
module FastInserter
|
45
44
|
class Base
|
46
|
-
DEFAULT_GROUP_SIZE =
|
45
|
+
DEFAULT_GROUP_SIZE = 1_000
|
47
46
|
|
48
47
|
def initialize(params)
|
49
48
|
@table_name = params[:table]
|
@@ -92,20 +91,33 @@ module FastInserter
|
|
92
91
|
end
|
93
92
|
end
|
94
93
|
|
94
|
+
def existing_values_sql(group_of_values)
|
95
|
+
sql = "SELECT #{@variable_columns.join(', ')} FROM #{@table_name} WHERE #{values_hash_to_sql(@static_columns)}"
|
96
|
+
|
97
|
+
if @variable_columns.length > 1
|
98
|
+
group_of_values_sql = group_of_values.map do |group|
|
99
|
+
values_hash = variable_column_values_to_hash(group)
|
100
|
+
"(#{values_hash_to_sql(values_hash)})"
|
101
|
+
end.join(' OR ')
|
102
|
+
|
103
|
+
sql += " AND (#{group_of_values_sql})"
|
104
|
+
else
|
105
|
+
values_to_check = ActiveRecord::Base.send(:sanitize_sql_array, ['?', group_of_values.flatten])
|
106
|
+
sql += " AND #{@variable_columns.first} IN (#{values_to_check})"
|
107
|
+
end
|
108
|
+
|
109
|
+
sql
|
110
|
+
end
|
111
|
+
|
95
112
|
# Queries for the existing values for a given group of values
|
96
113
|
def existing_values(group_of_values)
|
97
|
-
sql =
|
114
|
+
sql = existing_values_sql(group_of_values)
|
98
115
|
|
99
116
|
# NOTE: There are more elegant ways to get this field out of the resultset, but each database adaptor returns a different type
|
100
117
|
# of result from 'execute(sql)'. Potential classes for 'result' is Array (sqlite), Mysql2::Result (mysql2), PG::Result (pg). Each
|
101
118
|
# result can be enumerated into a list of arrays (mysql) or list of hashes (sqlite, pg)
|
102
119
|
results = ActiveRecord::Base.connection.execute(sql)
|
103
|
-
|
104
|
-
|
105
|
-
# Rather than a giant IN query in the sql statement (which can be bad for database performance),
|
106
|
-
# do the filtering of relevant values here in a ruby select.
|
107
|
-
group_of_values_strings = stringify_values(group_of_values)
|
108
|
-
existing_values & group_of_values_strings
|
120
|
+
stringify_values(results)
|
109
121
|
end
|
110
122
|
|
111
123
|
def stringify_values(results)
|
@@ -118,8 +130,18 @@ module FastInserter
|
|
118
130
|
end
|
119
131
|
end
|
120
132
|
|
121
|
-
def
|
122
|
-
|
133
|
+
def variable_column_values_to_hash(values)
|
134
|
+
hash = {}
|
135
|
+
|
136
|
+
@variable_columns.each_with_index do |variable_column, index|
|
137
|
+
hash[variable_column] = values[index]
|
138
|
+
end
|
139
|
+
|
140
|
+
hash
|
141
|
+
end
|
142
|
+
|
143
|
+
def values_hash_to_sql(values)
|
144
|
+
values.map do |key, value|
|
123
145
|
if value.nil?
|
124
146
|
"#{key} IS NULL"
|
125
147
|
else
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fast_inserter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0.pre
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Ringwelski
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: exe
|
14
14
|
cert_chain: []
|
15
|
-
date:
|
15
|
+
date: 2019-01-07 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: activerecord
|
@@ -137,15 +137,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
137
137
|
requirements:
|
138
138
|
- - ">="
|
139
139
|
- !ruby/object:Gem::Version
|
140
|
-
version: 2.
|
140
|
+
version: 2.3.8
|
141
141
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
142
|
requirements:
|
143
|
-
- - "
|
143
|
+
- - ">"
|
144
144
|
- !ruby/object:Gem::Version
|
145
|
-
version:
|
145
|
+
version: 1.3.1
|
146
146
|
requirements: []
|
147
147
|
rubyforge_project:
|
148
|
-
rubygems_version: 2.
|
148
|
+
rubygems_version: 2.7.6
|
149
149
|
signing_key:
|
150
150
|
specification_version: 4
|
151
151
|
summary: Quickly insert database records in bulk
|