Module: PGSpecHelper::MaterializedViews

Included in:
PGSpecHelper
Defined in:
lib/pg_spec_helper/materialized_views.rb

Defined Under Namespace

Classes: MaterializedViewNotTrackedError

Instance Method Summary collapse

Instance Method Details

#refresh_all_materialized_viewsObject

refresh all materialized views that have been tracked



34
35
36
37
38
39
40
41
42
# File 'lib/pg_spec_helper/materialized_views.rb', line 34

def refresh_all_materialized_views
  @materialized_views&.each do |schema_name, views|
    views.each do |materialized_view_name, view|
      if materialized_view_exists? schema_name, materialized_view_name
        refresh_materialized_view schema_name, materialized_view_name
      end
    end
  end
end

#track_materialized_view(schema_name, materialized_view_name, refresh_after) ⇒ Object

add the name of a materialized view to a list of views which will be refreshed after each type of change



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pg_spec_helper/materialized_views.rb', line 10

def track_materialized_view schema_name, materialized_view_name, refresh_after
  @materialized_views ||= {}
  @materialized_views[schema_name.to_sym] ||= {}

  # ensure the refresh_after contains at least one trackable method
  unless refresh_after.is_a?(Array) && refresh_after.count
    raise ArgumentError, "refresh_after must be an array of trackable method names"
  end

  # ensure each method in the refresh_after list is trackable
  refresh_after.each do |method_name|
    assert_trackable_method_name! method_name
  end

  @materialized_views[schema_name.to_sym][materialized_view_name.to_sym] = {
    # assume does not exist until proven otherwise
    exists: false,
    # list of methods which should trigger a refresh of this
    # materialized view
    refresh_after: refresh_after
  }
end