Class: ActiveRecord::Materialized::WriteChange

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord/materialized/write_change.rb

Overview

A committed write to a dependency table, captured as full before/after attribute snapshots (string-keyed). Snapshots are complete — not just the changed columns — so maintenance can compute deltas even when the GROUP BY key or a summed column did not change.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name:, operation:, before:, after:, source_ts: nil) ⇒ WriteChange

Returns a new instance of WriteChange.



18
19
20
21
22
23
24
# File 'lib/activerecord/materialized/write_change.rb', line 18

def initialize(table_name:, operation:, before:, after:, source_ts: nil)
  @table_name = table_name
  @operation = operation
  @before = before
  @after = after
  @source_ts = source_ts # optional monotonic CDC watermark (e.g. Debezium ts_ms); nil for in-app writes
end

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



16
17
18
# File 'lib/activerecord/materialized/write_change.rb', line 16

def after
  @after
end

#beforeObject (readonly)

Returns the value of attribute before.



16
17
18
# File 'lib/activerecord/materialized/write_change.rb', line 16

def before
  @before
end

#operationObject (readonly)

Returns the value of attribute operation.



16
17
18
# File 'lib/activerecord/materialized/write_change.rb', line 16

def operation
  @operation
end

#source_tsObject (readonly)

Returns the value of attribute source_ts.



16
17
18
# File 'lib/activerecord/materialized/write_change.rb', line 16

def source_ts
  @source_ts
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



16
17
18
# File 'lib/activerecord/materialized/write_change.rb', line 16

def table_name
  @table_name
end

Class Method Details

.from_descriptor(table_name:, operation:, key_attributes: nil, before: nil, after: nil) ⇒ Object

Builds a change from a normalized descriptor rather than an ActiveRecord record — the seam a CDC / external change stream feeds. Supply full +before+/+after+ images when available, or key_attributes (the GROUP BY columns) to scope maintenance to a partition; with neither — or a partial image that cannot identify every affected partition — it widens to a full recompute. Snapshot keys may be strings or symbols; an unknown operation is rejected.



49
50
51
52
53
54
55
56
# File 'lib/activerecord/materialized/write_change.rb', line 49

def self.from_descriptor(table_name:, operation:, key_attributes: nil, before: nil, after: nil)
  op = operation.to_sym
  raise_unsupported_operation!(operation) unless OPERATIONS.include?(op)

  resolved_before, resolved_after = snapshots_for(op, key_attributes, before, after)
  new(table_name: table_name, operation: op,
      before: stringify_keys(resolved_before), after: stringify_keys(resolved_after))
end

.from_record(record, operation) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/activerecord/materialized/write_change.rb', line 26

def self.from_record(record, operation)
  table_name = record.class.table_name
  case operation.to_sym
  when :create
    from_descriptor(table_name: table_name, operation: :create, after: record.attributes)
  when :update
    from_descriptor(table_name: table_name, operation: :update,
                    before: old_attributes(record), after: record.attributes)
  when :destroy
    # attributes_in_database is emptied once the row is gone; use in-memory attributes.
    from_descriptor(table_name: table_name, operation: :destroy, before: record.attributes)
  else
    raise_unsupported_operation!(operation)
  end
end

Instance Method Details

#with_source_ts(source_ts) ⇒ WriteChange

A copy of this change stamped with a CDC source watermark. Kept off from_descriptor so its (public) keyword list stays within the argument-count limit; ActiveRecord::Materialized.ingest_change applies it. See SourceWatermark.

Parameters:

  • source_ts (Integer)

    a monotonic per-partition source watermark

Returns:



64
65
66
67
# File 'lib/activerecord/materialized/write_change.rb', line 64

def with_source_ts(source_ts)
  self.class.new(table_name: table_name, operation: operation,
                 before: before, after: after, source_ts: source_ts)
end