Module: ActiveRecord::Materialized::ViewRefreshPolicyClassMethods::ClassMethods

Included in:
ActiveRecord::Materialized::ViewConfigurationClassMethods::ClassMethods
Defined in:
lib/activerecord/materialized/view_refresh_policy_class_methods.rb

Overview

The refresh-policy DSL methods available on a ActiveRecord::Materialized::View subclass.

Instance Method Summary collapse

Instance Method Details

#change_source(source) ⇒ void

This method returns an undefined value.

Selects where this view's changes come from: :callbacks (the default built-in tracker installs commit callbacks on depends_on models) or :none (install no callbacks; feed changes through the public ingestion API from an external adapter — e.g. a CDC stream).

Setting :callbacks (re)installs callbacks for any dependencies already declared, so it works regardless of whether it precedes or follows depends_on — important when the global default is :none.

Parameters:

  • source (Symbol)

    :callbacks or :none



69
70
71
72
# File 'lib/activerecord/materialized/view_refresh_policy_class_methods.rb', line 69

def change_source(source)
  @change_source = ChangeSource.cast(source)
  DependencyRegistry.install_callbacks_for(view_class) if @change_source == ChangeSource::CALLBACKS
end

#cold_read(strategy) ⇒ void

This method returns an undefined value.

Sets how reads are served before the view has been materialized.

Parameters:

  • strategy (Symbol)

    one of :read_through (default), :serve_stale, or :raise



49
50
51
# File 'lib/activerecord/materialized/view_refresh_policy_class_methods.rb', line 49

def cold_read(strategy)
  @cold_read_strategy = strategy.to_sym
end

#max_staleness(duration = nil, &block) ⇒ void

This method returns an undefined value.

Sets the maximum staleness window before the view is treated as stale — a static duration, or a block evaluated in the view's context for a dynamic window.

Parameters:

  • duration (Integer, ActiveSupport::Duration, nil) (defaults to: nil)

    a static staleness window (seconds)

Yield Returns:

  • (Integer, ActiveSupport::Duration)

    a dynamically computed staleness window



121
122
123
124
125
126
127
128
129
# File 'lib/activerecord/materialized/view_refresh_policy_class_methods.rb', line 121

def max_staleness(duration = nil, &block)
  setting = duration || block
  unless setting.nil? || [Integer, ::ActiveSupport::Duration, Proc].any? { |type| setting.is_a?(type) }
    raise ArgumentError,
          "max_staleness expects an Integer (seconds), a Duration, or a block, got #{duration.inspect}"
  end

  @max_staleness_setting = setting
end

#refresh_debounce(seconds) ⇒ void

This method returns an undefined value.

Sets how long successive async refreshes are coalesced before one runs.

Parameters:

  • seconds (Numeric, ActiveSupport::Duration)

    the debounce interval



37
38
39
40
41
42
43
# File 'lib/activerecord/materialized/view_refresh_policy_class_methods.rb', line 37

def refresh_debounce(seconds)
  unless seconds.is_a?(Numeric) || seconds.is_a?(::ActiveSupport::Duration)
    raise ArgumentError, "refresh_debounce expects seconds (a number or Duration), got #{seconds.inspect}"
  end

  @refresh_debounce = seconds
end

#refresh_on_change(strategy = :async) ⇒ void

This method returns an undefined value.

Sets the strategy used to refresh the view when a dependency changes.

Parameters:

  • strategy (Symbol) (defaults to: :async)

    one of :async (default), :immediate, or :manual

Raises:

  • (ArgumentError)

    if strategy is not a known refresh strategy



23
24
25
26
27
28
29
30
31
# File 'lib/activerecord/materialized/view_refresh_policy_class_methods.rb', line 23

def refresh_on_change(strategy = :async)
  strategy = strategy.to_sym
  unless RefreshScheduler::STRATEGIES.include?(strategy)
    raise ArgumentError,
          "unknown refresh strategy #{strategy.inspect}; expected one of #{RefreshScheduler::STRATEGIES.inspect}"
  end

  @refresh_strategy = strategy
end

#resolved_change_sourceObject



74
75
76
77
# File 'lib/activerecord/materialized/view_refresh_policy_class_methods.rb', line 74

def resolved_change_source
  @change_source ||
    ActiveRecord::Materialized.configuration.default_change_source
end

#resolved_cold_read_strategyObject



53
54
55
56
# File 'lib/activerecord/materialized/view_refresh_policy_class_methods.rb', line 53

def resolved_cold_read_strategy
  @cold_read_strategy ||
    ActiveRecord::Materialized.configuration.default_cold_read_strategy
end

#resolved_max_stalenessObject



131
132
133
134
135
136
137
138
# File 'lib/activerecord/materialized/view_refresh_policy_class_methods.rb', line 131

def resolved_max_staleness
  setting = @max_staleness_setting
  default = ActiveRecord::Materialized.configuration.default_max_staleness
  return default if setting.nil?
  return view_class.instance_eval(&setting) if setting.is_a?(Proc)

  setting
end

#resolved_refresh_debounceObject



106
107
108
109
110
111
112
113
# File 'lib/activerecord/materialized/view_refresh_policy_class_methods.rb', line 106

def resolved_refresh_debounce
  interval = if @refresh_debounce.nil?
               ActiveRecord::Materialized.configuration.default_refresh_debounce
             else
               @refresh_debounce
             end
  interval.respond_to?(:to_f) ? interval.to_f : interval.to_i
end

#resolved_refresh_strategyObject



102
103
104
# File 'lib/activerecord/materialized/view_refresh_policy_class_methods.rb', line 102

def resolved_refresh_strategy
  @refresh_strategy || ActiveRecord::Materialized.configuration.default_refresh_strategy
end

#resolved_warm_up_queriesObject



88
89
90
91
92
93
# File 'lib/activerecord/materialized/view_refresh_policy_class_methods.rb', line 88

def resolved_warm_up_queries
  block = @warm_up_definition
  return [] if block.nil?

  Kernel.Array(view_class.instance_eval(&block))
end

#view_classObject



14
15
16
# File 'lib/activerecord/materialized/view_refresh_policy_class_methods.rb', line 14

def view_class
  self
end

#warm_up(&block) ⇒ void

This method returns an undefined value.

Queries warm_up! runs to materialize a cold view's hot partitions, e.g.:

warm_up { [where(region: "us"), order(revenue: :desc).limit(50)] }

Yield Returns:

  • (Array<ActiveRecord::Relation>)

    the relations whose partitions to warm



84
85
86
# File 'lib/activerecord/materialized/view_refresh_policy_class_methods.rb', line 84

def warm_up(&block)
  @warm_up_definition = block
end

#warm_up!Object

Running each warm_up query enqueues scoped maintenance for the partitions it touches; refresh! then applies it.



97
98
99
100
# File 'lib/activerecord/materialized/view_refresh_policy_class_methods.rb', line 97

def warm_up!
  resolved_warm_up_queries.each(&:to_a)
  view_class.refresh!
end