db_fuel 1.2.1.pre.alpha.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +10 -10
- data/lib/db_fuel/library/active_record/base.rb +2 -2
- data/lib/db_fuel/library/active_record/find_or_insert.rb +8 -6
- data/lib/db_fuel/library/active_record/insert.rb +5 -6
- data/lib/db_fuel/library/active_record/update.rb +4 -4
- data/lib/db_fuel/library/active_record/update_all.rb +1 -1
- data/lib/db_fuel/library/active_record/upsert.rb +17 -16
- data/lib/db_fuel/modeling/attribute_renderer_set.rb +14 -27
- data/lib/db_fuel/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 532f508d6074e44c1dd4bb57dcecf343f32e035d44f6bdbfcb0405127ede5552
|
4
|
+
data.tar.gz: dccd390d0c15277ab9ca6c93027d12db85ebe7ad970333ebc2eadc7fc6f39b8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b49ad96e45049b564625a2288786e26087701e84c063aa71b316fde794237006c430fb9cc101ef2704efd79117702c36427e3bdf41098622ddb747e2af01d2f0
|
7
|
+
data.tar.gz: 1d426794ea2045a5a6c1966352b1e9a00ae991cf4d2996315011da26c99fa652ab0ce719dc3a8cd9a495f61df350a1cc1584d7e47a3aa0317276fbea866e6f37
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
# 2.0.0 (March 16th, 2021)
|
2
|
+
|
3
|
+
New Jobs:
|
4
|
+
* db_fuel/active_record/upsert
|
5
|
+
* db_fuel/active_record/update_all
|
6
|
+
|
7
|
+
Changes:
|
8
|
+
* db_fuel/active_record/update now only updates a single record. Use db_fuel/active_record/update_all to update multiple records at a time.
|
9
|
+
|
1
10
|
# 1.1.0 (Decmeber 1st, 2020)
|
2
11
|
|
3
12
|
New Jobs:
|
data/README.md
CHANGED
@@ -24,11 +24,11 @@ Refer to the [Burner](https://github.com/bluemarblepayroll/burner) library for m
|
|
24
24
|
|
25
25
|
### ActiveRecord Jobs
|
26
26
|
|
27
|
-
* **db_fuel/active_record/find_or_insert** [name, table_name, attributes, debug,
|
28
|
-
* **db_fuel/active_record/insert** [name, table_name, attributes, debug,
|
27
|
+
* **db_fuel/active_record/find_or_insert** [name, table_name, attributes, debug, primary_keyed_column, register, separator, timestamps, unique_attributes]: An extension of the `db_fuel/active_record/insert` job that adds an existence check before SQL insertion. The `unique_attributes` will be converted to WHERE clauses for performing the existence check.
|
28
|
+
* **db_fuel/active_record/insert** [name, table_name, attributes, debug, primary_keyed_column, register, separator, timestamps]: This job can take the objects in a register and insert them into a database table. If primary_keyed_column is specified then its key will be set to the primary key. Note that composite primary keys are not supported. Attributes defines which object properties to convert to SQL. Refer to the class and constructor specification for more detail.
|
29
29
|
* **db_fuel/active_record/update_all** [name, table_name, attributes, debug, register, separator, timestamps, unique_attributes]: This job can take the objects in a register and updates them within a database table. Attributes defines which object properties to convert to SQL SET clauses while unique_attributes translate to WHERE clauses. One or more records may be updated at a time. Refer to the class and constructor specification for more detail.
|
30
|
-
* **db_fuel/active_record/update** [name, table_name, attributes, debug, register,
|
31
|
-
* **db_fuel/active_record/upsert** [name, table_name, attributes, debug,
|
30
|
+
* **db_fuel/active_record/update** [name, table_name, attributes, debug, register, primary_keyed_column, separator, timestamps, unique_attributes]: This job can take the unique objects in a register and updates them within a database table. Attributes defines which object properties to convert to SQL SET clauses while unique_attributes translate to WHERE clauses to find the records to update. The primary_keyed_column is used to update the unique record. Only one record will be updated per statement. Note that composite primary keys are not supported. Refer to the class and constructor specification for more detail.
|
31
|
+
* **db_fuel/active_record/upsert** [name, table_name, attributes, debug, primary_keyed_column, register, separator, timestamps, unique_attributes]: This job can take the objects in a register and either inserts or updates them within a database table. Attributes defines which object properties to convert to SQL SET clauses while each key in unique_attributes become a WHERE clause in order to check for the existence of a specific record. The updated record will use the primary_keyed_column specified to perform the UPDATE operation. Note that composite primary keys are not supported. Refer to the class and constructor specification for more detail.
|
32
32
|
|
33
33
|
### Dbee Jobs
|
34
34
|
|
@@ -187,7 +187,7 @@ pipeline = {
|
|
187
187
|
{ key: :last_name }
|
188
188
|
],
|
189
189
|
table_name: 'patients',
|
190
|
-
|
190
|
+
primary_keyed_column: {
|
191
191
|
key: :id
|
192
192
|
}
|
193
193
|
}
|
@@ -203,13 +203,13 @@ There should now be two new patients, AB0 and AB1, present in the table `patient
|
|
203
203
|
|
204
204
|
Notes:
|
205
205
|
|
206
|
-
* Since we specified the `
|
206
|
+
* Since we specified the `primary_keyed_column`, the records' `id` attributes should be set to their respective primary key values.
|
207
207
|
* Composite primary keys are not currently supported.
|
208
208
|
* Set `debug: true` to print out each INSERT statement in the output (not for production use.)
|
209
209
|
|
210
210
|
#### Inserting Only New Records
|
211
211
|
|
212
|
-
Another job `db_fuel/active_record/find_or_insert` allows for an existence check to performed each insertion. If a record is found then it will not insert the record. If `
|
212
|
+
Another job `db_fuel/active_record/find_or_insert` allows for an existence check to performed each insertion. If a record is found then it will not insert the record. If `primary_keyed_column` is set then the existence check will also still set the primary key on the payload's respective object. Note that composite primary keys are not currently supported. We can build on the above insert example for only inserting new patients if their chart_number is unique:
|
213
213
|
|
214
214
|
````ruby
|
215
215
|
pipeline = {
|
@@ -233,7 +233,7 @@ pipeline = {
|
|
233
233
|
{ key: :last_name }
|
234
234
|
],
|
235
235
|
table_name: 'patients',
|
236
|
-
|
236
|
+
primary_keyed_column: {
|
237
237
|
key: :id
|
238
238
|
},
|
239
239
|
unique_attributes: [
|
@@ -274,7 +274,7 @@ pipeline = {
|
|
274
274
|
{ key: :last_name }
|
275
275
|
],
|
276
276
|
table_name: 'patients',
|
277
|
-
|
277
|
+
primary_keyed_column: {
|
278
278
|
key: :id
|
279
279
|
},
|
280
280
|
unique_attributes: [
|
@@ -356,7 +356,7 @@ pipeline = {
|
|
356
356
|
{ key: :last_name }
|
357
357
|
],
|
358
358
|
table_name: 'patients',
|
359
|
-
|
359
|
+
primary_keyed_column: {
|
360
360
|
key: :id
|
361
361
|
},
|
362
362
|
unique_attributes: [
|
@@ -36,8 +36,8 @@ module DbFuel
|
|
36
36
|
super(name: name, register: register)
|
37
37
|
|
38
38
|
@resolver = Objectable.resolver(separator: separator)
|
39
|
-
@attribute_renderers_set = Modeling::AttributeRendererSet.new(
|
40
|
-
|
39
|
+
@attribute_renderers_set = Modeling::AttributeRendererSet.new(resolver: resolver,
|
40
|
+
attributes: attributes)
|
41
41
|
@db_provider = DbProvider.new(table_name)
|
42
42
|
@debug = debug || false
|
43
43
|
end
|
@@ -14,8 +14,9 @@ module DbFuel
|
|
14
14
|
module ActiveRecord
|
15
15
|
# This job is a slight enhancement to the insert job, in that it will only insert new
|
16
16
|
# records. It will use the unique_keys to first run a query to see if it exists.
|
17
|
-
# Each unique_key becomes a WHERE clause. If
|
18
|
-
# found then the first record's id will be set to
|
17
|
+
# Each unique_key becomes a WHERE clause. If primary_keyed_column is specified
|
18
|
+
# and a record is found then the first record's id will be set to
|
19
|
+
# the primary_keyed_column.
|
19
20
|
#
|
20
21
|
# Expected Payload[register] input: array of objects
|
21
22
|
# Payload[register] output: array of objects.
|
@@ -36,8 +37,9 @@ module DbFuel
|
|
36
37
|
# returned objects will be printed in the output. Only use this option while
|
37
38
|
# debugging issues as it will fill up the output with (potentially too much) data.
|
38
39
|
#
|
39
|
-
#
|
40
|
-
#
|
40
|
+
# primary_keyed_column: If primary_keyed_column is present then it will be used to set
|
41
|
+
# the object's property to the returned primary key
|
42
|
+
# from the INSERT statement.
|
41
43
|
#
|
42
44
|
# separator: Just like other jobs with a 'separator' option, if the objects require
|
43
45
|
# key-path notation or nested object support, you can set the separator
|
@@ -55,7 +57,7 @@ module DbFuel
|
|
55
57
|
name: '',
|
56
58
|
attributes: [],
|
57
59
|
debug: false,
|
58
|
-
|
60
|
+
primary_keyed_column: nil,
|
59
61
|
register: Burner::DEFAULT_REGISTER,
|
60
62
|
separator: '',
|
61
63
|
timestamps: true,
|
@@ -67,7 +69,7 @@ module DbFuel
|
|
67
69
|
table_name: table_name,
|
68
70
|
attributes: attributes,
|
69
71
|
debug: debug,
|
70
|
-
|
72
|
+
primary_keyed_column: primary_keyed_column,
|
71
73
|
register: register,
|
72
74
|
separator: separator,
|
73
75
|
timestamps: timestamps,
|
@@ -17,8 +17,6 @@ module DbFuel
|
|
17
17
|
# Expected Payload[register] input: array of objects
|
18
18
|
# Payload[register] output: array of objects.
|
19
19
|
class Insert < Upsert
|
20
|
-
# attr_reader :primary_key
|
21
|
-
|
22
20
|
# Arguments:
|
23
21
|
# name: name of the job within the Burner::Pipeline.
|
24
22
|
#
|
@@ -33,8 +31,9 @@ module DbFuel
|
|
33
31
|
# returned objects will be printed in the output. Only use this option while
|
34
32
|
# debugging issues as it will fill up the output with (potentially too much) data.
|
35
33
|
#
|
36
|
-
#
|
37
|
-
#
|
34
|
+
# primary_keyed_column: If primary_keyed_column is present then it will be used to
|
35
|
+
# set the object's property to the returned primary key
|
36
|
+
# from the INSERT statement.
|
38
37
|
#
|
39
38
|
# separator: Just like other jobs with a 'separator' option, if the objects require
|
40
39
|
# key-path notation or nested object support, you can set the separator
|
@@ -49,7 +48,7 @@ module DbFuel
|
|
49
48
|
name: '',
|
50
49
|
attributes: [],
|
51
50
|
debug: false,
|
52
|
-
|
51
|
+
primary_keyed_column: nil,
|
53
52
|
register: Burner::DEFAULT_REGISTER,
|
54
53
|
separator: '',
|
55
54
|
timestamps: true
|
@@ -62,7 +61,7 @@ module DbFuel
|
|
62
61
|
table_name: table_name,
|
63
62
|
attributes: attributes,
|
64
63
|
debug: debug,
|
65
|
-
|
64
|
+
primary_keyed_column: primary_keyed_column,
|
66
65
|
register: register,
|
67
66
|
separator: separator,
|
68
67
|
timestamps: timestamps
|
@@ -15,7 +15,7 @@ module DbFuel
|
|
15
15
|
# This job can take the unique objects in a register and updates them within database table.
|
16
16
|
# The attributes translate to SQL SET clauses and the unique_keys translate to
|
17
17
|
# WHERE clauses to find the records to update.
|
18
|
-
# The
|
18
|
+
# The primary_keyed_column is used to update the unique record.
|
19
19
|
# Only one record will be updated per statement.
|
20
20
|
#
|
21
21
|
# Expected Payload[register] input: array of objects
|
@@ -35,7 +35,7 @@ module DbFuel
|
|
35
35
|
# returned objects will be printed in the output. Only use this option while
|
36
36
|
# debugging issues as it will fill up the output with (potentially too much) data.
|
37
37
|
#
|
38
|
-
#
|
38
|
+
# primary_keyed_column [required]: Primary key column for the corresponding table.
|
39
39
|
# Used as the WHERE clause for the UPDATE statement.
|
40
40
|
# Only one record will be updated at a time
|
41
41
|
# using the primary key specified.
|
@@ -56,7 +56,7 @@ module DbFuel
|
|
56
56
|
name: '',
|
57
57
|
attributes: [],
|
58
58
|
debug: false,
|
59
|
-
|
59
|
+
primary_keyed_column: nil,
|
60
60
|
register: Burner::DEFAULT_REGISTER,
|
61
61
|
separator: '',
|
62
62
|
timestamps: true,
|
@@ -70,7 +70,7 @@ module DbFuel
|
|
70
70
|
table_name: table_name,
|
71
71
|
attributes: attributes,
|
72
72
|
debug: debug,
|
73
|
-
|
73
|
+
primary_keyed_column: primary_keyed_column,
|
74
74
|
register: register,
|
75
75
|
separator: separator,
|
76
76
|
timestamps: timestamps,
|
@@ -16,12 +16,12 @@ module DbFuel
|
|
16
16
|
# It will use the unique_keys to first run a query to see if it exists.
|
17
17
|
# Each unique_key becomes a WHERE clause. If a record is found it will then
|
18
18
|
# update the found record using the primary key specified.
|
19
|
-
# If a record is updated or created the record's id will be set to the
|
19
|
+
# If a record is updated or created the record's id will be set to the primary_keyed_column.
|
20
20
|
#
|
21
21
|
# Expected Payload[register] input: array of objects
|
22
22
|
# Payload[register] output: array of objects.
|
23
23
|
class Upsert < Base
|
24
|
-
attr_reader :
|
24
|
+
attr_reader :primary_keyed_column, :timestamps, :unique_attribute_renderers
|
25
25
|
|
26
26
|
# Arguments:
|
27
27
|
# name: name of the job within the Burner::Pipeline.
|
@@ -38,9 +38,10 @@ module DbFuel
|
|
38
38
|
# debugging issues as it will fill
|
39
39
|
# up the output with (potentially too much) data.
|
40
40
|
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
41
|
+
# primary_keyed_column [required]: Used to set the object's property to the
|
42
|
+
# returned primary key from the
|
43
|
+
# INSERT statement or used as the
|
44
|
+
# WHERE clause for the UPDATE statement.
|
44
45
|
#
|
45
46
|
# separator: Just like other jobs with a 'separator' option, if the objects require
|
46
47
|
# key-path notation or nested object support, you can set the separator
|
@@ -57,7 +58,7 @@ module DbFuel
|
|
57
58
|
# order to check for the existence of a specific record.
|
58
59
|
def initialize(
|
59
60
|
table_name:,
|
60
|
-
|
61
|
+
primary_keyed_column:,
|
61
62
|
name: '',
|
62
63
|
attributes: [],
|
63
64
|
debug: false,
|
@@ -75,10 +76,10 @@ module DbFuel
|
|
75
76
|
separator: separator
|
76
77
|
)
|
77
78
|
|
78
|
-
@
|
79
|
+
@primary_keyed_column = Modeling::KeyedColumn.make(primary_keyed_column, nullable: true)
|
79
80
|
|
80
81
|
@unique_attribute_renderers = attribute_renderers_set
|
81
|
-
.
|
82
|
+
.make_renderers(unique_attributes)
|
82
83
|
|
83
84
|
@timestamps = timestamps
|
84
85
|
|
@@ -86,7 +87,7 @@ module DbFuel
|
|
86
87
|
end
|
87
88
|
|
88
89
|
def perform(output, payload)
|
89
|
-
raise ArgumentError, '
|
90
|
+
raise ArgumentError, 'primary_keyed_column is required' unless primary_keyed_column
|
90
91
|
|
91
92
|
total_inserted = 0
|
92
93
|
total_updated = 0
|
@@ -118,9 +119,9 @@ module DbFuel
|
|
118
119
|
|
119
120
|
first_record = db_provider.first(unique_row)
|
120
121
|
|
121
|
-
id = resolver.get(first_record,
|
122
|
+
id = resolver.get(first_record, primary_keyed_column.column)
|
122
123
|
|
123
|
-
resolver.set(row,
|
124
|
+
resolver.set(row, primary_keyed_column.key, id)
|
124
125
|
|
125
126
|
debug_detail(output, "Record Exists: #{first_record}") if first_record
|
126
127
|
|
@@ -144,24 +145,24 @@ module DbFuel
|
|
144
145
|
|
145
146
|
id = db_provider.insert(set_object)
|
146
147
|
|
147
|
-
# add the primary key name and value to row if
|
148
|
-
resolver.set(row,
|
148
|
+
# add the primary key name and value to row if primary_keyed_column was specified
|
149
|
+
resolver.set(row, primary_keyed_column.key, id) if primary_keyed_column
|
149
150
|
|
150
151
|
debug_detail(output, "Insert Return: #{row}")
|
151
152
|
end
|
152
153
|
|
153
154
|
# Updates only a single record. Lookups primary key to update the record.
|
154
155
|
def update_record(output, row, time)
|
155
|
-
raise ArgumentError, '
|
156
|
+
raise ArgumentError, 'primary_keyed_column is required' unless primary_keyed_column
|
156
157
|
|
157
158
|
first_record = find_record(output, row, time)
|
158
159
|
|
159
160
|
if first_record
|
160
161
|
debug_detail(output, "Record Exists: #{first_record}")
|
161
162
|
|
162
|
-
id = resolver.get(first_record,
|
163
|
+
id = resolver.get(first_record, primary_keyed_column.column)
|
163
164
|
|
164
|
-
where_object = {
|
165
|
+
where_object = { primary_keyed_column.column => id }
|
165
166
|
|
166
167
|
# update record using the primary key as the WHERE clause
|
167
168
|
update(output, row, time, where_object)
|
@@ -12,25 +12,31 @@ module DbFuel
|
|
12
12
|
# Creates attribute renderers based on attributes passed.
|
13
13
|
# Also constains methods to transform attribute renderers
|
14
14
|
# and include timestamp attributes if needed.
|
15
|
-
class AttributeRendererSet
|
16
|
-
CREATED_AT = :created_at
|
15
|
+
class AttributeRendererSet # :nodoc:
|
17
16
|
NOW_TYPE = 'r/value/now'
|
18
|
-
|
17
|
+
|
18
|
+
CREATED_AT = Burner::Modeling::Attribute.make(
|
19
|
+
key: :created_at, transformers: [{ type: NOW_TYPE }]
|
20
|
+
)
|
21
|
+
|
22
|
+
UPDATED_AT = Burner::Modeling::Attribute.make(
|
23
|
+
key: :updated_at, transformers: [{ type: NOW_TYPE }]
|
24
|
+
)
|
19
25
|
|
20
26
|
attr_reader :attribute_renderers, :resolver
|
21
27
|
|
22
|
-
def initialize(attributes: []
|
28
|
+
def initialize(resolver:, attributes: [])
|
23
29
|
raise ArgumentError, 'resolver is required' unless resolver
|
24
30
|
|
25
31
|
@resolver = resolver
|
26
|
-
@attribute_renderers =
|
32
|
+
@attribute_renderers = make_renderers(attributes)
|
27
33
|
|
28
34
|
freeze
|
29
35
|
end
|
30
36
|
|
31
37
|
# Adds the attributes for created_at and updated_at to the currrent attribute renderers.
|
32
38
|
def timestamp_created_attribute_renderers
|
33
|
-
timestamp_attributes = [
|
39
|
+
timestamp_attributes = [CREATED_AT, UPDATED_AT]
|
34
40
|
|
35
41
|
timestamp_attributes.map do |a|
|
36
42
|
Burner::Modeling::AttributeRenderer.new(a, resolver)
|
@@ -39,14 +45,14 @@ module DbFuel
|
|
39
45
|
|
40
46
|
# Adds the attribute for updated_at to the currrent attribute renderers.
|
41
47
|
def timestamp_updated_attribute_renderers
|
42
|
-
timestamp_attributes = [
|
48
|
+
timestamp_attributes = [UPDATED_AT]
|
43
49
|
|
44
50
|
timestamp_attributes.map do |a|
|
45
51
|
Burner::Modeling::AttributeRenderer.new(a, resolver)
|
46
52
|
end + attribute_renderers
|
47
53
|
end
|
48
54
|
|
49
|
-
def
|
55
|
+
def make_renderers(attributes)
|
50
56
|
Burner::Modeling::Attribute
|
51
57
|
.array(attributes)
|
52
58
|
.map { |a| Burner::Modeling::AttributeRenderer.new(a, resolver) }
|
@@ -59,25 +65,6 @@ module DbFuel
|
|
59
65
|
resolver.set(memo, attribute_renderer.key, value)
|
60
66
|
end
|
61
67
|
end
|
62
|
-
|
63
|
-
private
|
64
|
-
|
65
|
-
def created_at_timestamp_attribute
|
66
|
-
timestamp_attribute(CREATED_AT)
|
67
|
-
end
|
68
|
-
|
69
|
-
def updated_at_timestamp_attribute
|
70
|
-
timestamp_attribute(UPDATED_AT)
|
71
|
-
end
|
72
|
-
|
73
|
-
def timestamp_attribute(key)
|
74
|
-
Burner::Modeling::Attribute.make(
|
75
|
-
key: key,
|
76
|
-
transformers: [
|
77
|
-
{ type: NOW_TYPE }
|
78
|
-
]
|
79
|
-
)
|
80
|
-
end
|
81
68
|
end
|
82
69
|
end
|
83
70
|
end
|
data/lib/db_fuel/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db_fuel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Ruggio
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2021-
|
12
|
+
date: 2021-03-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -274,9 +274,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
274
274
|
version: '2.5'
|
275
275
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
276
276
|
requirements:
|
277
|
-
- - "
|
277
|
+
- - ">="
|
278
278
|
- !ruby/object:Gem::Version
|
279
|
-
version:
|
279
|
+
version: '0'
|
280
280
|
requirements: []
|
281
281
|
rubygems_version: 3.0.3
|
282
282
|
signing_key:
|