debezium 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 +4 -4
- data/lib/debezium/change.rb +38 -61
- data/lib/debezium/message.rb +13 -22
- data/lib/debezium/version.rb +1 -1
- 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: c0e00e705b2f701951ea8404510a46fea597a3ff5888144895dcecfb53ecdbdd
|
4
|
+
data.tar.gz: 84ad023f6885d4a0c3b1c1a0f2bf1d8e764677981cd6acab41c9f153e4c0dc66
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94a9907de3bcebb41de108fbe75be7e0eb94979b5aaab4b03af80b3e87144b988a71b2e042b8a11668a8a97ba2ee95f5664c7f26e92ce59625ef9c762cf2107d
|
7
|
+
data.tar.gz: f5720a98be5733a0953c88fe7ea426c6c568ba1c476871e5b46fbec44669c2326d2f8276815bbd92f41421e24964625bba6329ec52513b9b99b8ce4773e6f00e
|
data/lib/debezium/change.rb
CHANGED
@@ -3,16 +3,19 @@
|
|
3
3
|
module Debezium
|
4
4
|
# Represents the differences between two hashes, categorizing them as additions, removals, or modifications.
|
5
5
|
class Change
|
6
|
+
# @return [Object, nil] The `after` state of the record.
|
7
|
+
attr_reader :after
|
8
|
+
|
9
|
+
# @return [Object, nil] The `before` state of the record.
|
10
|
+
attr_reader :before
|
11
|
+
|
6
12
|
# Initializes a Change object and computes the differences between two hashes.
|
7
13
|
#
|
8
|
-
# @param
|
9
|
-
# @param
|
10
|
-
def initialize(
|
11
|
-
@
|
12
|
-
@
|
13
|
-
@modifications = {}
|
14
|
-
|
15
|
-
hash_diff(old, new)
|
14
|
+
# @param before [Hash] The original hash.
|
15
|
+
# @param after [Hash] The modified hash.
|
16
|
+
def initialize(before, after)
|
17
|
+
@before = before
|
18
|
+
@after = after
|
16
19
|
end
|
17
20
|
|
18
21
|
# Checks if the provided key was added.
|
@@ -20,7 +23,7 @@ module Debezium
|
|
20
23
|
# @param key [Object] The key to check.
|
21
24
|
# @return [Boolean] True if the key was added, otherwise false.
|
22
25
|
def added?(key)
|
23
|
-
|
26
|
+
additions.include?(key)
|
24
27
|
end
|
25
28
|
|
26
29
|
# Checks if the provided key was removed.
|
@@ -28,7 +31,7 @@ module Debezium
|
|
28
31
|
# @param key [Object] The key to check.
|
29
32
|
# @return [Boolean] True if the key was removed, otherwise false.
|
30
33
|
def removed?(key)
|
31
|
-
|
34
|
+
removals.include?(key)
|
32
35
|
end
|
33
36
|
|
34
37
|
# Checks if the provided key was modified.
|
@@ -36,81 +39,55 @@ module Debezium
|
|
36
39
|
# @param key [Object] The key to check.
|
37
40
|
# @return [Boolean] True if the key was modified, otherwise false.
|
38
41
|
def modified?(key)
|
39
|
-
|
42
|
+
modifications.include?(key)
|
40
43
|
end
|
41
44
|
|
42
45
|
# Returns the list of added key-value pairs.
|
43
46
|
#
|
44
|
-
# @return [
|
47
|
+
# @return [Hash{Symbol => Object] A hash where keys represent added attributes and values are the added value
|
45
48
|
def additions
|
46
|
-
@additions
|
49
|
+
@additions ||= find_additions
|
47
50
|
end
|
48
51
|
|
49
|
-
# Returns the list of
|
52
|
+
# Returns the list of added key-value pairs.
|
50
53
|
#
|
51
|
-
# @return [
|
54
|
+
# @return [Hash{Symbol => Object] A hash where keys represent removed attributes and values are the previous value
|
52
55
|
def removals
|
53
|
-
@removals
|
56
|
+
@removals ||= find_removals
|
54
57
|
end
|
55
58
|
|
56
59
|
# Returns the list of modified key-value pairs.
|
57
60
|
#
|
58
|
-
# @return [Array<
|
61
|
+
# @return [Hash{Symbol => Array<Object>] A hash of where keys represent modified attributes
|
62
|
+
# and values are a tuple of the previous and next value.
|
59
63
|
def modifications
|
60
|
-
@modifications
|
64
|
+
@modifications ||= find_modifications
|
61
65
|
end
|
62
66
|
|
63
67
|
private
|
64
68
|
|
65
|
-
|
66
|
-
|
67
|
-
# @param old_hash [Hash] The original hash.
|
68
|
-
# @param new_hash [Hash] The modified hash.
|
69
|
-
# @return [void]
|
70
|
-
def hash_diff(old_hash, new_hash)
|
71
|
-
hash_additions(old_hash, new_hash)
|
72
|
-
hash_removals(old_hash, new_hash)
|
73
|
-
hash_modifications(old_hash, new_hash)
|
74
|
-
end
|
69
|
+
def find_additions
|
70
|
+
return {} if @before.nil? || @after.nil?
|
75
71
|
|
76
|
-
|
77
|
-
#
|
78
|
-
# @param old_hash [Hash] The original hash.
|
79
|
-
# @param new_hash [Hash] The modified hash.
|
80
|
-
# @return [void]
|
81
|
-
def hash_additions(old_hash, new_hash)
|
82
|
-
new_hash.each do |key, value|
|
83
|
-
next if old_hash.key?(key)
|
84
|
-
|
85
|
-
@additions[key] = value
|
86
|
-
end
|
72
|
+
@after.reject { |key| @before.key?(key) }
|
87
73
|
end
|
88
74
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
# @return [void]
|
94
|
-
def hash_removals(old_hash, new_hash)
|
95
|
-
old_hash.each do |key, value|
|
96
|
-
next if new_hash.key?(key)
|
97
|
-
|
98
|
-
@removals[key] = value
|
99
|
-
end
|
75
|
+
def find_removals
|
76
|
+
return {} if @before.nil? || @after.nil?
|
77
|
+
|
78
|
+
@before.reject { |key| @after.key?(key) }
|
100
79
|
end
|
101
80
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
next if new_hash[key] == value
|
111
|
-
|
112
|
-
@modifications[key] = [new_hash[key], value]
|
81
|
+
def find_modifications
|
82
|
+
return {} if @before.nil? || @after.nil?
|
83
|
+
|
84
|
+
@before.filter_map do |key, before_value|
|
85
|
+
after_value = @after[key]
|
86
|
+
next if removals.key?(key) || after_value == before_value
|
87
|
+
|
88
|
+
[key, [before_value, after_value]]
|
113
89
|
end
|
90
|
+
.to_h
|
114
91
|
end
|
115
92
|
end
|
116
93
|
end
|
data/lib/debezium/message.rb
CHANGED
@@ -8,28 +8,17 @@ module Debezium
|
|
8
8
|
# of operation and access the changes between the `before` and `after` states in case of an update operation.
|
9
9
|
#
|
10
10
|
class Message
|
11
|
-
# @return [Object] The `after` state of the record.
|
12
|
-
attr_accessor :after
|
13
|
-
|
14
|
-
# @return [Object] The `before` state of the record.
|
15
|
-
attr_accessor :before
|
16
|
-
|
17
11
|
# @return [Symbol] The operation type (`:create`, `:update`, `:delete`, or `:unknown`).
|
18
|
-
|
19
|
-
|
20
|
-
# @return [Hash] The parsed JSON of the event.
|
21
|
-
attr_accessor :json
|
12
|
+
attr_reader :op
|
22
13
|
|
23
14
|
# Initializes a new Message instance by parsing the given Debezium JSON message.
|
24
15
|
#
|
25
16
|
# @param json [String] The Debezium JSON message to parse.
|
26
17
|
# @return [Message] The newly created Message instance.
|
27
18
|
def initialize(json)
|
28
|
-
@json
|
29
|
-
|
30
|
-
@
|
31
|
-
@after = @json['after']
|
32
|
-
@op = parse_op(@json['op'])
|
19
|
+
@json = JSON.parse(json)
|
20
|
+
@payload = @json['payload']
|
21
|
+
@op = parse_op
|
33
22
|
end
|
34
23
|
|
35
24
|
# Checks if the operation is a "create" operation.
|
@@ -53,23 +42,25 @@ module Debezium
|
|
53
42
|
@op == :delete
|
54
43
|
end
|
55
44
|
|
56
|
-
# Returns the changes between the `before` and `after` states
|
45
|
+
# Returns the changes between the `before` and `after` states.
|
57
46
|
#
|
58
|
-
# @return [Change
|
47
|
+
# @return [Change] Returns a `Change` object.
|
59
48
|
def changes
|
60
|
-
|
49
|
+
@changes ||= Change.new(@payload['before'], @payload['after'])
|
50
|
+
end
|
61
51
|
|
62
|
-
|
52
|
+
# @return [Hash] The parsed JSON of the event.
|
53
|
+
def to_h
|
54
|
+
@json
|
63
55
|
end
|
64
56
|
|
65
57
|
private
|
66
58
|
|
67
59
|
# Parses the operation type from the given Debezium operation code.
|
68
60
|
#
|
69
|
-
# @param operation [String] The Debezium operation code (`'c'` for create, `'u'` for update, `'d'` for delete).
|
70
61
|
# @return [Symbol] The operation (`:create`, `:update`, `:delete`, or `:unknown`).
|
71
|
-
def parse_op
|
72
|
-
case
|
62
|
+
def parse_op
|
63
|
+
case @payload['op']
|
73
64
|
when 'c'
|
74
65
|
:create
|
75
66
|
when 'u'
|
data/lib/debezium/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: debezium
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tate Thurston
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-03-
|
11
|
+
date: 2025-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|