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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: fe09e32230a3dc433d82f2afe7329cfe6388c46d
4
- data.tar.gz: ea29978534d0cb33c67baafa43fbcd1f8d02735a
2
+ SHA256:
3
+ metadata.gz: 3bbe7d518954dce221c2fecd334c6441f664b9c02041a4c41dc99f231af6801d
4
+ data.tar.gz: '028e339eb3b132caf9cb397c1c9e13e5505b346654ed6a01146c7e1ec7da5ed0'
5
5
  SHA512:
6
- metadata.gz: 03556b9df683ce5c6bce7797af5d6754a001167aeae82bd4edc43b2ed2eb0fc21a23c15f32fb740a1fe884a846660ccebda1cc514dd1c048fc6b619b0470ca61
7
- data.tar.gz: 389035369e1874a8f522a0c5dbd0be6ddc6e9f16dbf6088101021e05def715d5c204dc49dacb278e8405f4a328c72a7739c472bd3698ada9d233140ed826c0a2
6
+ metadata.gz: 93828bca03374757e898a731bb33ca9f957f391ef06e1e967ae662874ce49d2a4fdfc75feab2557168299cb22c8bd5ce70d480eeaa9e4ffa0d104a97dda24fef
7
+ data.tar.gz: efd8eb195e7556e5020edd88341a1f56f1c5f036720e5c4b7ca08dbb5579f56e05a56d651753865d806116bd312fd05b917154f7f36d067c4eabbbd216576a75
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.3.0
1
+ 2.5.3
data/.travis.yml CHANGED
@@ -3,9 +3,9 @@ sudo: required
3
3
  notifications:
4
4
  email: false
5
5
  rvm:
6
- - 2.3.0
7
- - 2.4.4
8
- - 2.5.1
6
+ - 2.3.8
7
+ - 2.4.5
8
+ - 2.5.3
9
9
  - ruby-head
10
10
  env:
11
11
  matrix:
data/CHANGELOG.md CHANGED
@@ -1,4 +1,14 @@
1
- ## Unreleased ##
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 2,000.
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
 
@@ -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.2.0"
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. Default 10_000.
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 = 2_000
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 = "SELECT #{@variable_columns.join(', ')} FROM #{@table_name} WHERE #{existing_values_static_columns}"
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
- existing_values = stringify_values(results)
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 existing_values_static_columns
122
- @static_columns.map do |key, value|
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
@@ -1,3 +1,3 @@
1
1
  module FastInserter
2
- VERSION = "0.1.6"
2
+ VERSION = '1.0.0.pre'.freeze
3
3
  end
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.6
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: 2018-07-03 00:00:00.000000000 Z
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.2.0
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: '0'
145
+ version: 1.3.1
146
146
  requirements: []
147
147
  rubyforge_project:
148
- rubygems_version: 2.5.1
148
+ rubygems_version: 2.7.6
149
149
  signing_key:
150
150
  specification_version: 4
151
151
  summary: Quickly insert database records in bulk