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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69ce62068d2d8b3b158d5c6a617c978eafa4bb9dad851c8890c1a55f305ba9e8
|
4
|
+
data.tar.gz: 13b0a05be8df3ed3d0686761c215b5b92159c4bf91a21338a35e651ae0d0fe3b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d1c46decaeb5d17180ffc6ce2ef0c011d446d23e922aec43730bd1944d0b198f29a01638568301a16a1b527577a7d532b4f21f70be364446e77ae2974bbff22
|
7
|
+
data.tar.gz: 8c4aa172c4fea27ee0cfe4a5ab24770dbaa254fb4efbf3d7f99e7aa85ccf1cf7142929ade8db3228149431f057e1d06a406e63f63c6e948623dd78e2db6292a5
|
@@ -9,22 +9,16 @@ module BetterBatch
|
|
9
9
|
@model = model
|
10
10
|
end
|
11
11
|
|
12
|
-
def upsert(data, unique_by:, returning:)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
27
|
-
data.zip(
|
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
|
-
|
39
|
-
|
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
|
-
|
43
|
-
when nil
|
42
|
+
rows.map(&:first)
|
43
|
+
when nil, []
|
44
44
|
nil
|
45
45
|
else
|
46
|
-
hash_rows(query.returning,
|
46
|
+
hash_rows(query.returning, rows)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
50
|
def with_selected_pk(data, unique_by:)
|
51
|
-
|
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
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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
|
76
|
-
|
77
|
-
|
78
|
-
|
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
|
82
|
-
|
83
|
-
|
84
|
-
|
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,
|
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
|
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.
|
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
|