fast_inserter 0.1.4 → 0.1.5
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 +4 -4
- data/README.md +9 -1
- data/lib/fast_inserter/fast_inserter_base.rb +11 -11
- data/lib/fast_inserter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 120b53eb90d8eef25a9d1f3340b36f4256d9ddca
|
4
|
+
data.tar.gz: aea33ef6b99f3707b2276785cc5add4d74522020
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e47da657ffab5623bae32686880e916f507b49bd9a33067be2c80ee1131bf269430dc7a2a670045acd478d5a0a16f9d7c1afd971fc1aace6b96db19b89a7d102
|
7
|
+
data.tar.gz: b694592a8c016b52f39993cd3695316d383868f6cf267b84f50682e4eab662ab5f123a0b041ef1f3759b9d7c620593cb9fb83a98dd7e23c2cbc63bf658779780
|
data/README.md
CHANGED
@@ -99,6 +99,15 @@ The name of the column which we will be dynamically inserting records for. This
|
|
99
99
|
|
100
100
|
The large list of values to use for the 'variable_column' value when inserting the records.
|
101
101
|
|
102
|
+
## Multiple Variable Columns
|
103
|
+
Rather than a single `variable_column`, you may pass an array of `variable_columns`, along with `values` as an array of arrays.
|
104
|
+
|
105
|
+
Example:
|
106
|
+
```
|
107
|
+
variable_columns: %w(user_id user_email)
|
108
|
+
values: [[1, 'foo@example.com'], [2, 'bar@example.com']]
|
109
|
+
```
|
110
|
+
|
102
111
|
## Development
|
103
112
|
|
104
113
|
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.
|
@@ -113,4 +122,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/stryde
|
|
113
122
|
## License
|
114
123
|
|
115
124
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
116
|
-
|
@@ -49,13 +49,13 @@ module FastInserter
|
|
49
49
|
@table_name = params[:table]
|
50
50
|
@static_columns = params[:static_columns]
|
51
51
|
@additional_columns = params[:additional_columns]
|
52
|
-
@
|
52
|
+
@variable_columns = Array(params[:variable_columns] || params[:variable_column])
|
53
53
|
@options = params[:options] || {}
|
54
54
|
|
55
55
|
# We want to break up the insertions into multiple transactiosn in case there
|
56
56
|
# is a very large amount of values. This avoids PG:OutOfMemory errors and smooths
|
57
57
|
# out the load. The second 'false' param means don't fill in the last group with nil elements.
|
58
|
-
all_values = params[:values]
|
58
|
+
all_values = params[:values].map { |value| Array(value) }
|
59
59
|
all_values.uniq! if @options[:unique]
|
60
60
|
group_size = Integer(params[:group_size] || ENV['FAST_INSERTER_GROUP_SIZE'] || DEFAULT_GROUP_SIZE)
|
61
61
|
@value_groups = all_values.in_groups_of(group_size, false)
|
@@ -82,7 +82,7 @@ module FastInserter
|
|
82
82
|
def fast_insert_group(group)
|
83
83
|
if @options[:check_for_existing]
|
84
84
|
ActiveRecord::Base.transaction do
|
85
|
-
non_existing_values = group.map(&:to_s) - existing_values(group)
|
85
|
+
non_existing_values = group.map { |values| values.map(&:to_s) } - existing_values(group)
|
86
86
|
sql_string = insertion_sql_for_group(non_existing_values)
|
87
87
|
ActiveRecord::Base.connection.execute(sql_string) unless non_existing_values.empty?
|
88
88
|
end
|
@@ -94,7 +94,7 @@ module FastInserter
|
|
94
94
|
|
95
95
|
# Queries for the existing values for a given group of values
|
96
96
|
def existing_values(group_of_values)
|
97
|
-
sql = "SELECT #{@
|
97
|
+
sql = "SELECT #{@variable_columns.join(', ')} FROM #{@table_name} WHERE #{existing_values_static_columns}"
|
98
98
|
|
99
99
|
# NOTE: There are more elegant ways to get this field out of the resultset, but each database adaptor returns a different type
|
100
100
|
# of result from 'execute(sql)'. Potential classes for 'result' is Array (sqlite), Mysql2::Result (mysql2), PG::Result (pg). Each
|
@@ -102,15 +102,15 @@ module FastInserter
|
|
102
102
|
results = ActiveRecord::Base.connection.execute(sql)
|
103
103
|
existing_values = results.to_a.map do |result|
|
104
104
|
if result.is_a?(Hash)
|
105
|
-
result[
|
105
|
+
@variable_columns.map { |col| result[col] }.map(&:to_s)
|
106
106
|
elsif result.is_a?(Array)
|
107
|
-
result
|
107
|
+
result.map(&:to_s)
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
111
|
# Rather than a giant IN query in the sql statement (which can be bad for database performance),
|
112
112
|
# do the filtering of relevant values here in a ruby select.
|
113
|
-
group_of_values_strings = group_of_values.map(&:to_s)
|
113
|
+
group_of_values_strings = group_of_values.map { |values| values.map(&:to_s) }
|
114
114
|
existing_values & group_of_values_strings
|
115
115
|
end
|
116
116
|
|
@@ -130,7 +130,7 @@ module FastInserter
|
|
130
130
|
end
|
131
131
|
|
132
132
|
def column_names
|
133
|
-
"#{all_static_columns.keys.join(', ')}, #{@
|
133
|
+
"#{all_static_columns.keys.join(', ')}, #{@variable_columns.join(', ')}"
|
134
134
|
end
|
135
135
|
|
136
136
|
def all_static_columns
|
@@ -157,9 +157,9 @@ module FastInserter
|
|
157
157
|
rv = []
|
158
158
|
static_column_values = ActiveRecord::Base.send(:sanitize_sql_array, ["?", all_static_columns.values])
|
159
159
|
|
160
|
-
group_of_values.each do |
|
161
|
-
|
162
|
-
rv << "(#{static_column_values},#{
|
160
|
+
group_of_values.each do |values|
|
161
|
+
values = values.map { |value| ActiveRecord::Base.send(:sanitize_sql_array, ["?", value]) }
|
162
|
+
rv << "(#{static_column_values},#{values.join(',')})"
|
163
163
|
end
|
164
164
|
|
165
165
|
rv.join(', ')
|
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.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Ringwelski
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: exe
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2017-01-09 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activerecord
|