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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d27374b4f6388c2eae44b4b9654b10461c8fd7d044f5a67a63d7b1866b8658f
4
- data.tar.gz: 1b058d23b6e5bbab471b57f74d0f85c3cd3c78131c9b43a32494f1b95b3c9110
3
+ metadata.gz: e6496a3f7011b2d9cf92bc2841bd0cdaeea6173eb8a2fb4f388c640ecc619f0d
4
+ data.tar.gz: d98cd1b64bcc56397b4be95a54018beb57d294803ef3d2dec4f0ce649471f217
5
5
  SHA512:
6
- metadata.gz: fb7e58230650beaec9d777a8ee8e33a9210b08ef05d59f64e6e3a9c5f8cfb095af72f15a443063998d3c6fd579263f1b25d0409b371200fbe9d95bee43ab0300
7
- data.tar.gz: a6198b80dd040db5f2b633fc5e8c83da727f6d9e0d9915684df00f9f1e55f73fec99ed8ff482ac5f37378d1ebae18ebf7d9d24b6b89051625274b2f70b9cfbc4
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.2.0.pre.beta1)
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.9.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.66.0)
45
- exponential-backoff (0.0.2)
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.13.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.pre.beta2)
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.unscoped.where(klass.primary_key => key).first
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 ||= klass.new
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!
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Deimos
4
- VERSION = '1.2.0-beta1'
4
+ VERSION = '1.3.0-beta1'
5
5
  end
@@ -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.2.0.pre.beta1
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-09-12 00:00:00.000000000 Z
11
+ date: 2019-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro-patches