Class: Mammoth::PersistedPayloadDeserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/mammoth/persisted_payload_deserializer.rb

Overview

Reconstructs exact CDC core work items from persisted webhook payloads.

Dead letters and sample files contain Mammoth's JSON delivery projection, not live CDC objects. This class is the explicit boundary that converts those persisted representations back into core vocabulary.

Class Method Summary collapse

Class Method Details

.event(payload) ⇒ CDC::Core::ChangeEvent

Deserialize one persisted event payload.

Parameters:

  • payload (Hash)

    Mammoth event payload

Returns:

  • (CDC::Core::ChangeEvent)

    reconstructed core event



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mammoth/persisted_payload_deserializer.rb', line 17

def event(payload)
  attributes = stringify_keys(payload)
  data = attributes["data"]
   = (attributes)

  CDC::Core::ChangeEvent.new(
    operation: attributes.fetch("operation"),
    schema: attributes["namespace"] || attributes.fetch("schema"),
    table: attributes["entity"] || attributes.fetch("table"),
    old_values: attributes["old_values"] || delete_values(attributes, data),
    new_values: attributes["new_values"] || changed_values(attributes, data),
    primary_key: attributes["identity"] || attributes["primary_key"],
    transaction_id: attributes["transaction_id"],
    commit_lsn: attributes["commit_lsn"] || attributes["source_position"],
    sequence_number: attributes["sequence_number"],
    occurred_at: parse_time(attributes["occurred_at"]),
    metadata: 
  )
rescue KeyError, ArgumentError, TypeError => e
  raise ConfigurationError, "invalid persisted CDC event: #{e.message}"
end

.transaction(payload) ⇒ CDC::Core::TransactionEnvelope

Deserialize one persisted transaction payload.

Parameters:

  • payload (Hash)

    Mammoth transaction webhook payload

Returns:

  • (CDC::Core::TransactionEnvelope)

    reconstructed core transaction



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mammoth/persisted_payload_deserializer.rb', line 43

def transaction(payload)
  attributes = stringify_keys(payload)
  CDC::Core::TransactionEnvelope.new(
    transaction_id: attributes.fetch("transaction_id"),
    events: attributes.fetch("events").map { |event_payload| event(event_payload) },
    commit_lsn: attributes["commit_lsn"] || attributes["source_position"],
    committed_at: parse_time(attributes["committed_at"]),
    metadata: (attributes)
  )
rescue KeyError, ArgumentError, TypeError => e
  raise ConfigurationError, "invalid persisted CDC transaction: #{e.message}"
end