deimos-ruby 1.2.0.pre.beta1 → 1.3.0.pre.beta1
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/CHANGELOG.md +3 -0
- data/Gemfile.lock +6 -6
- data/README.md +12 -0
- data/lib/deimos/active_record_consumer.rb +24 -3
- data/lib/deimos/version.rb +1 -1
- data/spec/active_record_consumer_spec.rb +36 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6496a3f7011b2d9cf92bc2841bd0cdaeea6173eb8a2fb4f388c640ecc619f0d
|
4
|
+
data.tar.gz: d98cd1b64bcc56397b4be95a54018beb57d294803ef3d2dec4f0ce649471f217
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 393b37b8dc0799317ffb037552dd3c6f140c45dfa8ed33de93fa2dcfaead6a06d4065ed2082b3026acbae7ae023494bfe09a5d1a9cb4ad08cb7620c2d00b11a5
|
7
|
+
data.tar.gz: e3474dda911ee613f4048f82d76983cc6d361e5b878c87514543cd8b6105f3bf5c8ecb255a1349a59efb9944f637dabee147d7e32a9b767f1e06a90512bccc79
|
data/CHANGELOG.md
CHANGED
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
7
7
|
|
8
8
|
## UNRELEASED
|
9
9
|
|
10
|
+
# [1.3.0-beta1] - 2019-11-21
|
11
|
+
- Added `fetch_record` and `assign_key` methods to ActiveRecordConsumer.
|
12
|
+
|
10
13
|
# [1.2.0-beta1] - 2019-09-12
|
11
14
|
- Added `fatal_error` to both global config and consumer classes.
|
12
15
|
- Changed `pending_db_messages_max_wait` metric to send per topic.
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
deimos-ruby (1.
|
4
|
+
deimos-ruby (1.3.0.pre.beta1)
|
5
5
|
avro-patches (~> 0.3)
|
6
6
|
avro_turf (~> 0.8)
|
7
7
|
phobos (~> 1.8.2.pre.beta2)
|
@@ -29,7 +29,7 @@ GEM
|
|
29
29
|
multi_json
|
30
30
|
avro-patches (0.4.1)
|
31
31
|
avro (= 1.8.2)
|
32
|
-
avro_turf (0.
|
32
|
+
avro_turf (0.11.0)
|
33
33
|
avro (>= 1.7.7, < 1.10)
|
34
34
|
excon (~> 0.45)
|
35
35
|
coderay (1.1.2)
|
@@ -41,8 +41,8 @@ GEM
|
|
41
41
|
diff-lcs (1.3)
|
42
42
|
digest-crc (0.4.1)
|
43
43
|
dogstatsd-ruby (4.2.0)
|
44
|
-
excon (0.
|
45
|
-
exponential-backoff (0.0.
|
44
|
+
excon (0.68.0)
|
45
|
+
exponential-backoff (0.0.4)
|
46
46
|
ffi (1.11.1)
|
47
47
|
formatador (0.2.5)
|
48
48
|
guard (2.14.2)
|
@@ -77,7 +77,7 @@ GEM
|
|
77
77
|
method_source (0.9.0)
|
78
78
|
minitest (5.11.3)
|
79
79
|
msgpack (1.2.10)
|
80
|
-
multi_json (1.
|
80
|
+
multi_json (1.14.1)
|
81
81
|
mysql2 (0.5.2)
|
82
82
|
nenv (0.3.0)
|
83
83
|
notiffany (0.1.1)
|
@@ -87,7 +87,7 @@ GEM
|
|
87
87
|
parser (2.6.3.0)
|
88
88
|
ast (~> 2.4.0)
|
89
89
|
pg (1.1.3)
|
90
|
-
phobos (1.8.2
|
90
|
+
phobos (1.8.2)
|
91
91
|
activesupport (>= 3.0.0)
|
92
92
|
concurrent-ruby (>= 1.0.2)
|
93
93
|
concurrent-ruby-ext (>= 1.0.2)
|
data/README.md
CHANGED
@@ -620,6 +620,18 @@ class MyConsumer < Deimos::ActiveRecordConsumer
|
|
620
620
|
key_config field: 'my_field'
|
621
621
|
record_class Widget
|
622
622
|
|
623
|
+
# Optional override of the way to fetch records based on payload and
|
624
|
+
# key. Default is to use the key to search the primary key of the table.
|
625
|
+
def fetch_record(klass, payload, key)
|
626
|
+
super
|
627
|
+
end
|
628
|
+
|
629
|
+
# Optional override on how to set primary key for new records.
|
630
|
+
# Default is to set the class's primary key to the message's decoded key.
|
631
|
+
def assign_key(record, payload, key)
|
632
|
+
super
|
633
|
+
end
|
634
|
+
|
623
635
|
# Optional override of the default behavior, which is to call `destroy`
|
624
636
|
# on the record - e.g. you can replace this with "archiving" the record
|
625
637
|
# in some way.
|
@@ -13,22 +13,43 @@ module Deimos
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
# Find the record specified by the given payload and key.
|
17
|
+
# Default is to use the primary key column and the value of the first
|
18
|
+
# field in the key.
|
19
|
+
# @param klass [Class < ActiveRecord::Base]
|
20
|
+
# @param _payload [Hash]
|
21
|
+
# @param key [Object]
|
22
|
+
# @return [ActiveRecord::Base]
|
23
|
+
def fetch_record(klass, _payload, key)
|
24
|
+
klass.unscoped.where(klass.primary_key => key).first
|
25
|
+
end
|
26
|
+
|
27
|
+
# Assign a key to a new record.
|
28
|
+
# @param record [ActiveRecord::Base]
|
29
|
+
# @param _payload [Hash]
|
30
|
+
# @param key [Object]
|
31
|
+
def assign_key(record, _payload, key)
|
32
|
+
record[record.class.primary_key] = key
|
33
|
+
end
|
34
|
+
|
16
35
|
# :nodoc:
|
17
36
|
def consume(payload, metadata)
|
18
37
|
key = metadata.with_indifferent_access[:key]
|
19
38
|
klass = self.class.config[:record_class]
|
20
|
-
record = klass
|
39
|
+
record = fetch_record(klass, (payload || {}).with_indifferent_access, key)
|
21
40
|
if payload.nil?
|
22
41
|
destroy_record(record)
|
23
42
|
return
|
24
43
|
end
|
25
|
-
record
|
44
|
+
if record.blank?
|
45
|
+
record = klass.new
|
46
|
+
assign_key(record, payload, key)
|
47
|
+
end
|
26
48
|
attrs = record_attributes(payload.with_indifferent_access)
|
27
49
|
# don't use attributes= - bypass Rails < 5 attr_protected
|
28
50
|
attrs.each do |k, v|
|
29
51
|
record.send("#{k}=", v)
|
30
52
|
end
|
31
|
-
record[klass.primary_key] = key
|
32
53
|
record.created_at ||= Time.zone.now if record.respond_to?(:created_at)
|
33
54
|
record.updated_at ||= Time.zone.now if record.respond_to?(:updated_at)
|
34
55
|
record.save!
|
data/lib/deimos/version.rb
CHANGED
@@ -36,6 +36,24 @@ module ActiveRecordProducerTest
|
|
36
36
|
end
|
37
37
|
stub_const('MyConsumer', consumer_class)
|
38
38
|
|
39
|
+
consumer_class = Class.new(Deimos::ActiveRecordConsumer) do
|
40
|
+
schema 'MySchema'
|
41
|
+
namespace 'com.my-namespace'
|
42
|
+
key_config none: true
|
43
|
+
record_class Widget
|
44
|
+
|
45
|
+
# :nodoc:
|
46
|
+
def assign_key(_record, _payload, _key)
|
47
|
+
# do nothing since we're not using primary keys
|
48
|
+
end
|
49
|
+
|
50
|
+
# :nodoc:
|
51
|
+
def fetch_record(klass, payload, _key)
|
52
|
+
klass.unscoped.where('test_id' => payload[:test_id]).first
|
53
|
+
end
|
54
|
+
end
|
55
|
+
stub_const('MyCustomFetchConsumer', consumer_class)
|
56
|
+
|
39
57
|
Time.zone = 'Eastern Time (US & Canada)'
|
40
58
|
end
|
41
59
|
|
@@ -83,5 +101,23 @@ module ActiveRecordProducerTest
|
|
83
101
|
|
84
102
|
end
|
85
103
|
|
104
|
+
it 'should find widgets by custom logic' do
|
105
|
+
widget1 = Widget.create!(test_id: 'id1')
|
106
|
+
expect(widget1.some_int).to be_nil
|
107
|
+
test_consume_message(MyCustomFetchConsumer, {
|
108
|
+
test_id: 'id1',
|
109
|
+
some_int: 3
|
110
|
+
}, { call_original: true })
|
111
|
+
expect(widget1.reload.some_int).to eq(3)
|
112
|
+
expect(Widget.count).to eq(1)
|
113
|
+
test_consume_message(MyCustomFetchConsumer, {
|
114
|
+
test_id: 'id2',
|
115
|
+
some_int: 4
|
116
|
+
}, { call_original: true })
|
117
|
+
expect(Widget.count).to eq(2)
|
118
|
+
expect(Widget.find_by_test_id('id1').some_int).to eq(3)
|
119
|
+
expect(Widget.find_by_test_id('id2').some_int).to eq(4)
|
120
|
+
end
|
121
|
+
|
86
122
|
end
|
87
123
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deimos-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0.pre.beta1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Orner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: avro-patches
|