better_batch-active_record 1.0.0 → 1.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c23e108b7e7d100bd763e2ccbd562877f702209940f8cb84e51922a763005bf1
4
- data.tar.gz: b50d42912bbbfb48481065286d39b4f3a4814905cafc1e256c44a5b98e25dfb3
3
+ metadata.gz: 69ce62068d2d8b3b158d5c6a617c978eafa4bb9dad851c8890c1a55f305ba9e8
4
+ data.tar.gz: 13b0a05be8df3ed3d0686761c215b5b92159c4bf91a21338a35e651ae0d0fe3b
5
5
  SHA512:
6
- metadata.gz: 075c1fbf58e1309cb0dca9c76d19ea372071f141e83f2f9b8bde265595b9fae7a142cff561aba4ca98fd94e20b791062c639078af6157aefba17057d41fa1f83
7
- data.tar.gz: 5a024d61cab1028350bf786eff66eab80b4c0d24add61170fdbdd05497c07f09d44fbdb86483b2ad923da0ebfbeb9c1eab65cbd3273ef3a16f91efb79fb5e74c
6
+ metadata.gz: 8d1c46decaeb5d17180ffc6ce2ef0c011d446d23e922aec43730bd1944d0b198f29a01638568301a16a1b527577a7d532b4f21f70be364446e77ae2974bbff22
7
+ data.tar.gz: 8c4aa172c4fea27ee0cfe4a5ab24770dbaa254fb4efbf3d7f99e7aa85ccf1cf7142929ade8db3228149431f057e1d06a406e63f63c6e948623dd78e2db6292a5
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BetterBatch
4
+ module ActiveRecord
5
+ class Error < StandardError; end
6
+ end
7
+ end
@@ -9,22 +9,16 @@ module BetterBatch
9
9
  @model = model
10
10
  end
11
11
 
12
- def upsert(data, unique_by:, returning:)
13
- query = build_query(data, unique_by:, returning:)
14
- result = exec_query(:upsert, query, data)
15
- case returning
16
- when Symbol
17
- result.rows.map(&:first)
18
- when nil
19
- nil
20
- else
21
- hash_rows(query.returning, result.rows)
22
- end
12
+ def upsert(data, unique_by:, except: nil, returning: nil)
13
+ upsert_data = slice_upsert(data, except:)
14
+ query = build_query(upsert_data, unique_by:, returning:)
15
+ result = exec_query(:upsert, query, upsert_data)
16
+ build_return(returning, result.rows, query)
23
17
  end
24
18
 
25
- def with_upserted_pk(data, unique_by:)
26
- query = build_query(data, unique_by:, returning: primary_key)
27
- data.zip(exec_query(:upsert, query, data).rows.map(&:first))
19
+ def with_upserted_pk(data, unique_by:, except: nil)
20
+ upserted = upsert(data, unique_by:, except:, returning: primary_key)
21
+ data.zip(upserted)
28
22
  end
29
23
 
30
24
  def set_upserted_pk(data, unique_by:)
@@ -35,21 +29,26 @@ module BetterBatch
35
29
  end
36
30
 
37
31
  def select(data, unique_by:, returning:)
38
- query = build_query(data, unique_by:, returning:)
39
- result = exec_query(:select, query, data)
32
+ assert_inputs_ok!(data, unique_by:)
33
+ select_data = data.map { |datum| datum.slice(*unique_by) }
34
+ query = build_query(select_data, unique_by:, returning:)
35
+ result = exec_query(:select, query, select_data)
36
+ build_return(returning, result.rows, query)
37
+ end
38
+
39
+ def build_return(returning, rows, query)
40
40
  case returning
41
41
  when Symbol
42
- result.rows.map(&:first)
43
- when nil
42
+ rows.map(&:first)
43
+ when nil, []
44
44
  nil
45
45
  else
46
- hash_rows(query.returning, result.rows)
46
+ hash_rows(query.returning, rows)
47
47
  end
48
48
  end
49
49
 
50
50
  def with_selected_pk(data, unique_by:)
51
- query = build_query(data, unique_by:, returning: primary_key)
52
- data.zip(exec_query(:select, query, data).rows.map(&:first))
51
+ data.zip(select(data, unique_by:, returning: primary_key))
53
52
  end
54
53
 
55
54
  def set_selected_pk(data, unique_by:)
@@ -63,31 +62,36 @@ module BetterBatch
63
62
 
64
63
  attr_reader :model
65
64
 
66
- def build_query(data, unique_by:, returning:)
67
- array_data = data.to_a
68
- unique_columns = Array(unique_by)
69
- input_columns = array_data.first.keys
70
- returning = Array(returning)
71
- BetterBatch::Query.new(table_name:, primary_key:, input_columns:, column_types:, unique_columns:,
72
- now_on_insert:, now_on_update:, returning:)
65
+ def slice_upsert(data, except:)
66
+ case except
67
+ when nil, []
68
+ data
69
+ else
70
+ data.map { |datum| datum.except(*except) }
71
+ end
73
72
  end
74
73
 
75
- def exec_query(type, query, data)
76
- sql = build_sql(type, query)
77
- json_data = JSON.generate(data)
78
- db_exec(sql, query, json_data)
74
+ def assert_inputs_ok!(data, unique_by:)
75
+ data_keys = data.first.keys
76
+ missing = Array(unique_by) - data_keys
77
+ return if missing.empty?
78
+
79
+ msg = "All unique_by columns must be in the given data, but #{missing.inspect} was missing from #{data_keys}."
80
+ raise Error, msg
79
81
  end
80
82
 
81
- def build_sql(type, query)
82
- query.public_send(type)
83
- rescue StandardError
84
- raise query.inspect
83
+ def build_query(data, unique_by:, returning:)
84
+ BetterBatch::Query.new(table_name:, primary_key:, input_columns: data.first.keys,
85
+ column_types:, unique_columns: unique_by, now_on_insert:,
86
+ now_on_update:, returning:)
87
+ end
88
+
89
+ def exec_query(type, query, data)
90
+ db_exec(query.public_send(type), JSON.generate(data))
85
91
  end
86
92
 
87
- def db_exec(sql, query, json_data)
93
+ def db_exec(sql, json_data)
88
94
  model.connection.exec_query(sql, nil, [json_data])
89
- rescue StandardError
90
- raise [query.inspect, query.upsert_formatted].join("\n")
91
95
  end
92
96
 
93
97
  def table_name
@@ -7,6 +7,6 @@ module BetterBatch
7
7
  # and trying to prevent it from autoloading was a struggle
8
8
  # so, as usual, the right thing to do is bend to Rails' will
9
9
  module Version; end
10
- VERSION = '1.0.0'
10
+ VERSION = '1.0.2'
11
11
  end
12
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_batch-active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Hartland
@@ -52,6 +52,7 @@ files:
52
52
  - lib/.DS_Store
53
53
  - lib/better_batch/.DS_Store
54
54
  - lib/better_batch/active_record.rb
55
+ - lib/better_batch/active_record/error.rb
55
56
  - lib/better_batch/active_record/model.rb
56
57
  - lib/better_batch/active_record/query.rb
57
58
  - lib/better_batch/active_record/version.rb