pgoutput-source-adapter 0.0.0
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 +7 -0
- data/.ruby-version +1 -0
- data/.yardopts +9 -0
- data/CHANGELOG.md +8 -0
- data/CODE_OF_CONDUCT.md +14 -0
- data/LICENSE.txt +21 -0
- data/README.md +156 -0
- data/Rakefile +29 -0
- data/lib/pgoutput/source/adapter.rb +15 -0
- data/lib/pgoutput/source_adapter/cdc.rb +232 -0
- data/lib/pgoutput/source_adapter/version.rb +10 -0
- data/lib/pgoutput/source_adapter.rb +27 -0
- data/mise.toml +2 -0
- data/sig/pgoutput/source/adapter.rbs +6 -0
- data/sig/pgoutput/source_adapter.rbs +9 -0
- metadata +88 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 9c0d3598bffe622103beb9a3f48c750476dcb06ec81b6d675d3a9b4628d61d1d
|
|
4
|
+
data.tar.gz: b4ab4d056f6d9f34620c88d78d28ddc6ccf678463b9975b3e4ad02328d7a61cb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 80770110f68b77ac080ebd48fe637f8bf7cb3e054e9c9345523535c27f3f8ab35f0c8e4d3ea5387aedbd6bdfc959795556644e2b5a18685c6749dd541ff2b3a7
|
|
7
|
+
data.tar.gz: '08b336516e7466179039bbc9a1a8874e96d70d3f9511566ae520e2417a373817ba716bbb20226e606ab532da1f6e00805bdb0da9a025f4d02f1a064bbb1bfdcd'
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.4
|
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
## [Unreleased]
|
|
2
|
+
|
|
3
|
+
## [0.1.0] - 2026-06-08
|
|
4
|
+
|
|
5
|
+
- Initial CDC source adapter implementation.
|
|
6
|
+
- Added `Pgoutput::SourceAdapter::Cdc`.
|
|
7
|
+
- Added normalization from `Pgoutput::Decoder::Events` to `CDC::Core::ChangeEvent`.
|
|
8
|
+
- Added transaction batch normalization to `CDC::Core::TransactionEnvelope`.
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
"cdc-core" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
|
+
|
|
5
|
+
- Participants will be tolerant of opposing views.
|
|
6
|
+
- Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
- When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
- Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
9
|
+
|
|
10
|
+
If you have concerns about behaviour within this project, please open an issue at:
|
|
11
|
+
|
|
12
|
+
https://github.com/kanutocd/cdc-core/issues/new
|
|
13
|
+
|
|
14
|
+
Please note that GitHub issues are public. Do not include sensitive personal information in your report.
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Keneth C. Demanawa
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# pgoutput-source-adapter
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/rb/pgoutput-source-adapter)
|
|
4
|
+
[](https://github.com/kanutocd/pgoutput-source-adapter/actions)
|
|
5
|
+
[](https://www.ruby-lang.org/en/)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
`pgoutput-source-adapter` adapts decoded pgoutput events into downstream change-event platform primitives.
|
|
9
|
+
|
|
10
|
+
The first supported target is the CDC Ecosystem:
|
|
11
|
+
|
|
12
|
+
```ruby
|
|
13
|
+
Pgoutput::SourceAdapter::Cdc
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
It normalizes `Pgoutput::Decoder::Events` into `CDC::Core::ChangeEvent` and `CDC::Core::TransactionEnvelope` objects.
|
|
17
|
+
|
|
18
|
+
## Boundary
|
|
19
|
+
|
|
20
|
+
The pgoutput family remains standalone:
|
|
21
|
+
|
|
22
|
+
```text
|
|
23
|
+
pgoutput-client -> PostgreSQL logical replication transport
|
|
24
|
+
pgoutput-parser -> pgoutput protocol messages
|
|
25
|
+
pgoutput-decoder -> typed Ruby row-change events
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
This gem is the adapter layer:
|
|
29
|
+
|
|
30
|
+
```text
|
|
31
|
+
Pgoutput::Decoder::Events
|
|
32
|
+
|
|
|
33
|
+
v
|
|
34
|
+
Pgoutput::SourceAdapter::Cdc
|
|
35
|
+
|
|
|
36
|
+
v
|
|
37
|
+
CDC::Core::ChangeEvent / TransactionEnvelope
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
That keeps the lower-level pgoutput gems usable outside the CDC Ecosystem while still providing a clean bridge into `cdc-core` for users building CDC platforms.
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
gem "pgoutput-source-adapter"
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
require "pgoutput/source_adapter"
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The generated `bundle gem` require path also works:
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
require "pgoutput/source/adapter"
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Usage
|
|
59
|
+
|
|
60
|
+
Normalize a decoded insert event:
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
adapter = Pgoutput::SourceAdapter::Cdc.new
|
|
64
|
+
change_event = adapter.normalize(decoded_insert)
|
|
65
|
+
|
|
66
|
+
change_event.operation
|
|
67
|
+
# => :insert
|
|
68
|
+
|
|
69
|
+
change_event.schema
|
|
70
|
+
change_event.table
|
|
71
|
+
change_event.new_values
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Normalize a transaction-shaped batch:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
results = adapter.normalize_many([
|
|
78
|
+
decoded_begin,
|
|
79
|
+
decoded_insert,
|
|
80
|
+
decoded_update,
|
|
81
|
+
decoded_commit
|
|
82
|
+
])
|
|
83
|
+
|
|
84
|
+
envelope = results.first
|
|
85
|
+
# => CDC::Core::TransactionEnvelope
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Primary keys
|
|
89
|
+
|
|
90
|
+
For update and delete events, pgoutput may provide an old-key tuple. When it does, that tuple is used as the `CDC::Core::ChangeEvent#primary_key`.
|
|
91
|
+
|
|
92
|
+
For insert events, or for sources without old-key tuples, the adapter defaults to `id` / `"id"` when present.
|
|
93
|
+
|
|
94
|
+
You can provide your own resolver:
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
adapter = Pgoutput::SourceAdapter::Cdc.new(
|
|
98
|
+
primary_key_resolver: ->(_event, values) { { "uuid" => values.fetch("uuid") } }
|
|
99
|
+
)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Metadata
|
|
103
|
+
|
|
104
|
+
Each normalized event includes pgoutput metadata:
|
|
105
|
+
|
|
106
|
+
```ruby
|
|
107
|
+
{
|
|
108
|
+
"source" => "pgoutput",
|
|
109
|
+
"relation_id" => 123,
|
|
110
|
+
"pgoutput_event" => "Insert"
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Additional metadata can be injected:
|
|
115
|
+
|
|
116
|
+
```ruby
|
|
117
|
+
adapter = Pgoutput::SourceAdapter::Cdc.new(
|
|
118
|
+
metadata_builder: ->(_event) { { pipeline: "default" } }
|
|
119
|
+
)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Public namespace
|
|
123
|
+
|
|
124
|
+
```ruby
|
|
125
|
+
Pgoutput::SourceAdapter
|
|
126
|
+
Pgoutput::SourceAdapter::Cdc
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
A compatibility alias is also provided for the generated gem path:
|
|
130
|
+
|
|
131
|
+
```ruby
|
|
132
|
+
Pgoutput::Source::Adapter
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Non-goals
|
|
136
|
+
|
|
137
|
+
This gem does not:
|
|
138
|
+
|
|
139
|
+
- connect to PostgreSQL
|
|
140
|
+
- parse pgoutput protocol messages
|
|
141
|
+
- decode PostgreSQL values
|
|
142
|
+
- run processors
|
|
143
|
+
- manage replication slots
|
|
144
|
+
- persist sink data
|
|
145
|
+
|
|
146
|
+
Those responsibilities belong to `pgoutput-client`, `pgoutput-parser`, `pgoutput-decoder`, runtime gems, or application code.
|
|
147
|
+
|
|
148
|
+
## Development
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
bundle exec rake
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
[MIT](./LICENSE.txt).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'bundler/gem_tasks'
|
|
4
|
+
require 'rake/testtask'
|
|
5
|
+
require 'rubocop/rake_task'
|
|
6
|
+
require 'yard'
|
|
7
|
+
|
|
8
|
+
Rake::TestTask.new(:test) do |t|
|
|
9
|
+
t.libs << 'lib'
|
|
10
|
+
t.libs << 'test'
|
|
11
|
+
t.test_files = FileList['test/**/*_test.rb']
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
RuboCop::RakeTask.new(:rubocop) do |task|
|
|
15
|
+
task.options = ['--parallel']
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
YARD::Rake::YardocTask.new(:yard)
|
|
19
|
+
|
|
20
|
+
task :steep do
|
|
21
|
+
sh 'bundle exec steep check'
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
desc 'Open coverage report'
|
|
25
|
+
task :coverage do
|
|
26
|
+
sh 'xdg-open coverage/index.html'
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
task default: %i[test rubocop steep yard]
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../source_adapter'
|
|
4
|
+
|
|
5
|
+
module Pgoutput
|
|
6
|
+
module Source
|
|
7
|
+
# Compatibility alias for {Pgoutput::SourceAdapter}.
|
|
8
|
+
#
|
|
9
|
+
# This alias preserves the generated bundle-gem path shape while the public
|
|
10
|
+
# adapter API lives under {Pgoutput::SourceAdapter}.
|
|
11
|
+
#
|
|
12
|
+
# @api public
|
|
13
|
+
Adapter = Pgoutput::SourceAdapter
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'cdc/core'
|
|
4
|
+
|
|
5
|
+
unless CDC::Core.const_defined?(:SourceAdapter)
|
|
6
|
+
raise LoadError, 'pgoutput-source-adapter requires a cdc-core version that defines CDC::Core::SourceAdapter'
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
module Pgoutput
|
|
10
|
+
module SourceAdapter
|
|
11
|
+
# Normalizes Pgoutput::Decoder::Events into CDC::Core primitives.
|
|
12
|
+
#
|
|
13
|
+
# This adapter is intentionally located under the Pgoutput namespace because
|
|
14
|
+
# it adapts pgoutput decoded events. The target is CDC::Core, so this class
|
|
15
|
+
# depends on cdc-core while the lower-level pgoutput-client, pgoutput-parser,
|
|
16
|
+
# and pgoutput-decoder gems remain standalone.
|
|
17
|
+
#
|
|
18
|
+
# @example Normalize a decoded insert event
|
|
19
|
+
# adapter = Pgoutput::SourceAdapter::Cdc.new
|
|
20
|
+
# change_event = adapter.normalize(decoded_insert)
|
|
21
|
+
#
|
|
22
|
+
# @example Normalize a decoded transaction event batch
|
|
23
|
+
# envelope = adapter.normalize_many([begin_event, insert_event, commit_event]).first
|
|
24
|
+
#
|
|
25
|
+
# @api public
|
|
26
|
+
class Cdc < CDC::Core::SourceAdapter
|
|
27
|
+
SOURCE_NAME = 'pgoutput'
|
|
28
|
+
|
|
29
|
+
# @param primary_key_resolver [#call, nil] optional callable used to infer
|
|
30
|
+
# primary keys from decoded row values when pgoutput does not provide an
|
|
31
|
+
# old-key tuple. The callable receives the decoded event and value hash.
|
|
32
|
+
# @param metadata_builder [#call, nil] optional callable that can return
|
|
33
|
+
# extra metadata for each decoded event. Returned keys are stringified.
|
|
34
|
+
# @return [void]
|
|
35
|
+
def initialize(primary_key_resolver: nil, metadata_builder: nil)
|
|
36
|
+
@primary_key_resolver = primary_key_resolver || method(:default_primary_key)
|
|
37
|
+
@metadata_builder = metadata_builder
|
|
38
|
+
super()
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Normalize one decoded pgoutput event.
|
|
42
|
+
#
|
|
43
|
+
# Transaction boundary events return nil because they do not represent a
|
|
44
|
+
# row-level change by themselves. Use #normalize_many when transaction
|
|
45
|
+
# envelopes are desired.
|
|
46
|
+
#
|
|
47
|
+
# @param event [Object] a Pgoutput::Decoder::Events object.
|
|
48
|
+
# @return [CDC::Core::ChangeEvent, nil] normalized row change event, or
|
|
49
|
+
# nil for transaction boundary events.
|
|
50
|
+
# @raise [Pgoutput::SourceAdapter::Error] when the decoded event type is
|
|
51
|
+
# unsupported.
|
|
52
|
+
def normalize(event)
|
|
53
|
+
case event_name(event)
|
|
54
|
+
when 'Insert'
|
|
55
|
+
change_event(
|
|
56
|
+
event,
|
|
57
|
+
operation: :insert,
|
|
58
|
+
old_values: nil,
|
|
59
|
+
new_values: event.values,
|
|
60
|
+
primary_key: primary_key_for(event, event.values)
|
|
61
|
+
)
|
|
62
|
+
when 'Update'
|
|
63
|
+
old_values = event.old_values || event.old_key
|
|
64
|
+
|
|
65
|
+
change_event(
|
|
66
|
+
event,
|
|
67
|
+
operation: :update,
|
|
68
|
+
old_values: old_values,
|
|
69
|
+
new_values: event.new_values,
|
|
70
|
+
primary_key: primary_key_for(event, event.new_values)
|
|
71
|
+
)
|
|
72
|
+
when 'Delete'
|
|
73
|
+
old_values = event.old_values || event.old_key
|
|
74
|
+
|
|
75
|
+
change_event(
|
|
76
|
+
event,
|
|
77
|
+
operation: :delete,
|
|
78
|
+
old_values: old_values,
|
|
79
|
+
new_values: nil,
|
|
80
|
+
primary_key: primary_key_for(event, old_values)
|
|
81
|
+
)
|
|
82
|
+
when 'Begin', 'Commit'
|
|
83
|
+
nil
|
|
84
|
+
else
|
|
85
|
+
raise Error, "unsupported pgoutput decoded event: #{event.class}"
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Normalize a sequence of decoded pgoutput events.
|
|
90
|
+
#
|
|
91
|
+
# If the sequence contains transaction boundaries, row changes between a
|
|
92
|
+
# Begin and Commit are grouped into CDC::Core::TransactionEnvelope. If no
|
|
93
|
+
# transaction boundaries are present, row changes are returned individually.
|
|
94
|
+
#
|
|
95
|
+
# @param events [Enumerable<Object>] decoded pgoutput events.
|
|
96
|
+
# @return [Array<CDC::Core::ChangeEvent, CDC::Core::TransactionEnvelope>]
|
|
97
|
+
# normalized row changes and transaction envelopes in input order.
|
|
98
|
+
# @raise [Pgoutput::SourceAdapter::Error] when any decoded event type is
|
|
99
|
+
# unsupported.
|
|
100
|
+
def normalize_many(events)
|
|
101
|
+
results = [] # : [Array[CDC::Core::ChangeEvent, CDC::Core::TransactionEnvelope]]
|
|
102
|
+
transaction_id = nil
|
|
103
|
+
transaction_events = []
|
|
104
|
+
transaction_metadata = {}
|
|
105
|
+
|
|
106
|
+
events.each do |event|
|
|
107
|
+
case event_name(event)
|
|
108
|
+
when 'Begin'
|
|
109
|
+
transaction_id = event.transaction_id
|
|
110
|
+
transaction_events = []
|
|
111
|
+
transaction_metadata = metadata_for(event).merge(
|
|
112
|
+
'begin_final_lsn' => lsn_string(event.final_lsn),
|
|
113
|
+
'begin_commit_timestamp' => event.commit_timestamp
|
|
114
|
+
)
|
|
115
|
+
when 'Commit'
|
|
116
|
+
if transaction_id || !transaction_events.empty?
|
|
117
|
+
results << transaction_envelope(
|
|
118
|
+
event,
|
|
119
|
+
transaction_id: transaction_id || event.transaction_id,
|
|
120
|
+
events: transaction_events,
|
|
121
|
+
metadata: transaction_metadata
|
|
122
|
+
)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
transaction_id = nil
|
|
126
|
+
transaction_events = []
|
|
127
|
+
transaction_metadata = {}
|
|
128
|
+
else
|
|
129
|
+
normalized = normalize(event)
|
|
130
|
+
next if normalized.nil?
|
|
131
|
+
|
|
132
|
+
if transaction_id
|
|
133
|
+
transaction_events << normalized
|
|
134
|
+
else
|
|
135
|
+
results << normalized
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
results.concat(transaction_events) if transaction_id && !transaction_events.empty?
|
|
141
|
+
share(results.freeze)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
private
|
|
145
|
+
|
|
146
|
+
attr_reader :primary_key_resolver, :metadata_builder
|
|
147
|
+
|
|
148
|
+
def change_event(event, operation:, old_values:, new_values:, primary_key:)
|
|
149
|
+
share(
|
|
150
|
+
CDC::Core::ChangeEvent.new(
|
|
151
|
+
operation: operation,
|
|
152
|
+
schema: event.schema,
|
|
153
|
+
table: event.table,
|
|
154
|
+
old_values: old_values,
|
|
155
|
+
new_values: new_values,
|
|
156
|
+
primary_key: primary_key,
|
|
157
|
+
transaction_id: event.transaction_id,
|
|
158
|
+
metadata: metadata_for(event)
|
|
159
|
+
)
|
|
160
|
+
)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def transaction_envelope(event, transaction_id:, events:, metadata:)
|
|
164
|
+
share(
|
|
165
|
+
CDC::Core::TransactionEnvelope.new(
|
|
166
|
+
transaction_id: transaction_id,
|
|
167
|
+
events: events.freeze,
|
|
168
|
+
commit_lsn: lsn_string(event.commit_lsn),
|
|
169
|
+
committed_at: event.commit_timestamp,
|
|
170
|
+
metadata: metadata.merge(metadata_for(event)).merge(
|
|
171
|
+
'commit_flags' => event.flags,
|
|
172
|
+
'transaction_end_lsn' => lsn_string(event.transaction_end_lsn)
|
|
173
|
+
)
|
|
174
|
+
)
|
|
175
|
+
)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def primary_key_for(event, values)
|
|
179
|
+
return event.old_key if event.respond_to?(:old_key) && event.old_key
|
|
180
|
+
|
|
181
|
+
primary_key_resolver.call(event, values)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def default_primary_key(_event, values)
|
|
185
|
+
return nil unless values.respond_to?(:key?)
|
|
186
|
+
|
|
187
|
+
if values.key?('id')
|
|
188
|
+
{ 'id' => values['id'] }
|
|
189
|
+
elsif values.key?(:id)
|
|
190
|
+
{ 'id' => values[:id] }
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def metadata_for(event)
|
|
195
|
+
metadata = {
|
|
196
|
+
'source' => SOURCE_NAME,
|
|
197
|
+
'relation_id' => relation_id_for(event),
|
|
198
|
+
'pgoutput_event' => event_name(event)
|
|
199
|
+
}
|
|
200
|
+
extra = metadata_builder&.call(event)
|
|
201
|
+
metadata.merge!(stringify_keys(extra)) if extra
|
|
202
|
+
compact(metadata)
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def relation_id_for(event)
|
|
206
|
+
event.relation_id if event.respond_to?(:relation_id)
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def event_name(event)
|
|
210
|
+
event.class.name.split('::').last
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def lsn_string(lsn)
|
|
214
|
+
lsn&.to_s
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def stringify_keys(hash)
|
|
218
|
+
hash.to_h.transform_keys(&:to_s)
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def compact(hash)
|
|
222
|
+
hash.compact
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def share(object)
|
|
226
|
+
Ractor.make_shareable(object)
|
|
227
|
+
rescue Ractor::Error
|
|
228
|
+
object
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'source_adapter/version'
|
|
4
|
+
|
|
5
|
+
module Pgoutput
|
|
6
|
+
# Source adapters normalize decoded pgoutput events into downstream platform
|
|
7
|
+
# event models.
|
|
8
|
+
#
|
|
9
|
+
# The pgoutput family remains independently useful outside the CDC Ecosystem.
|
|
10
|
+
# This namespace leaves room for adapters targeting other change-event
|
|
11
|
+
# platforms while providing {Pgoutput::SourceAdapter::Cdc} as the CDC
|
|
12
|
+
# Ecosystem adapter.
|
|
13
|
+
#
|
|
14
|
+
# @api public
|
|
15
|
+
module SourceAdapter
|
|
16
|
+
# Raised when a decoded pgoutput event cannot be normalized.
|
|
17
|
+
#
|
|
18
|
+
# The adapter raises this error for event objects outside the supported
|
|
19
|
+
# pgoutput decoder event family. Callers should treat it as a programming or
|
|
20
|
+
# integration error rather than a transient transport failure.
|
|
21
|
+
#
|
|
22
|
+
# @api public
|
|
23
|
+
class Error < StandardError; end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
require_relative 'source_adapter/cdc'
|
data/mise.toml
ADDED
metadata
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: pgoutput-source-adapter
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ken C. Demanawa
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: cdc-core
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0.1'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0.1'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: pgoutput-decoder
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0.1'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0.1'
|
|
40
|
+
description: Normalizes pgoutput decoded events into downstream change-event platform
|
|
41
|
+
primitives, including CDC::Core.
|
|
42
|
+
email:
|
|
43
|
+
- kenneth.c.demanawa@gmail.com
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- ".ruby-version"
|
|
49
|
+
- ".yardopts"
|
|
50
|
+
- CHANGELOG.md
|
|
51
|
+
- CODE_OF_CONDUCT.md
|
|
52
|
+
- LICENSE.txt
|
|
53
|
+
- README.md
|
|
54
|
+
- Rakefile
|
|
55
|
+
- lib/pgoutput/source/adapter.rb
|
|
56
|
+
- lib/pgoutput/source_adapter.rb
|
|
57
|
+
- lib/pgoutput/source_adapter/cdc.rb
|
|
58
|
+
- lib/pgoutput/source_adapter/version.rb
|
|
59
|
+
- mise.toml
|
|
60
|
+
- sig/pgoutput/source/adapter.rbs
|
|
61
|
+
- sig/pgoutput/source_adapter.rbs
|
|
62
|
+
homepage: https://github.com/kanutocd/pgoutput-source-adapter
|
|
63
|
+
licenses:
|
|
64
|
+
- MIT
|
|
65
|
+
metadata:
|
|
66
|
+
homepage_uri: https://github.com/kanutocd/pgoutput-source-adapter
|
|
67
|
+
source_code_uri: https://github.com/kanutocd/pgoutput-source-adapter
|
|
68
|
+
changelog_uri: https://github.com/kanutocd/pgoutput-source-adapter/blob/main/CHANGELOG.md
|
|
69
|
+
documentation_uri: https://kanutocd.github.io/pgoutput-source-adapter/
|
|
70
|
+
rubygems_mfa_required: 'true'
|
|
71
|
+
rdoc_options: []
|
|
72
|
+
require_paths:
|
|
73
|
+
- lib
|
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
|
+
requirements:
|
|
76
|
+
- - ">="
|
|
77
|
+
- !ruby/object:Gem::Version
|
|
78
|
+
version: 3.4.0
|
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
|
+
requirements:
|
|
81
|
+
- - ">="
|
|
82
|
+
- !ruby/object:Gem::Version
|
|
83
|
+
version: '0'
|
|
84
|
+
requirements: []
|
|
85
|
+
rubygems_version: 3.6.9
|
|
86
|
+
specification_version: 4
|
|
87
|
+
summary: Source adapters for pgoutput decoded events.
|
|
88
|
+
test_files: []
|