Class: ActiveRecord::Materialized::DataVerifier

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

Overview

Detects DATA drift — the materialized cache diverging from what the source relation would produce right now — as opposed to SchemaVerifier's schema drift. Stateless: it only reads and compares.

Modes trade cost for depth:

  • :row_count — which partitions exist (missing/extra); cheapest.
  • :checksum — a per-partition digest of the value columns.
  • :full — the value columns exactly.

sample: (Integer count / Float fraction) value-checks a random subset of partitions for large views; a sample covering every partition runs exhaustive.

Covers grouped/aggregate views whose GROUP BY keys map to projected columns (the incremental-maintenance target). A non-grouped view, or one whose group key can't be matched to a projected column, is skipped (an empty result).

Defined Under Namespace

Classes: DataDriftError

Constant Summary collapse

MODES =
%i[row_count checksum full].freeze

Instance Method Summary collapse

Constructor Details

#initialize(view_class, mode: :checksum, sample: nil) ⇒ DataVerifier

Returns a new instance of DataVerifier.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
# File 'lib/activerecord/materialized/data_verifier.rb', line 26

def initialize(view_class, mode: :checksum, sample: nil)
  raise ArgumentError, "unknown data-verification mode #{mode.inspect}" unless MODES.include?(mode)

  @view_class = view_class
  @mode = mode
  @sample = sample
end

Instance Method Details

#verifyObject



34
35
36
37
38
39
40
41
# File 'lib/activerecord/materialized/data_verifier.rb', line 34

def verify
  # verifiable? reads metadata/schema (and may provision the metadata table on first use), so it
  # runs on the ambient/primary connection — never the read-only replica. Only the cache-vs-source
  # comparison is routed to the verification role, inside one snapshot.
  return empty_result unless verifiable?

  ConnectionRouting.verification { in_consistent_snapshot { verify_within_snapshot } }
end