module Viewy::ActsAsMaterializedView

Provides a wrapper for materialized views that allows them and their dependencies to be refreshed easily

Public Instance Methods

full_table_name() click to toggle source
# File lib/viewy/acts_as_materialized_view.rb, line 51
        def full_table_name
  "#{schema_name}#{table_name}"
end
populated?() click to toggle source

Determines if a view has been populated (i.e. is in a queryable state)

@return [Boolean] true if the view has been populated, false if not

# File lib/viewy/acts_as_materialized_view.rb, line 40
      def populated?
        query = <<-SQL
          SELECT ispopulated 
          FROM pg_matviews 
          WHERE matviewname = '#{table_name}' 
            AND schemaname = '#{schema_name.chomp('.')}';
        SQL
        result = connection.execute query
        ActiveRecord::Type::Boolean.new.cast(result.values[0][0])
      end
refresh!(concurrently: false) click to toggle source

Refreshes this view and all materialized views it depends on. NOTE: the look-up for dependencies can take a second to run.

@raise [ActiveRecord::RecordNotFoundError] raised when the view refresh! is called on does not exist @raise [ActiveRecord::StatementInvalidError] raised if a dependent view is somehow not refreshed correctly

@return [PG::Result] the result of the refresh statement on the materialized view

# File lib/viewy/acts_as_materialized_view.rb, line 14
def refresh!(concurrently: false)
  refresher = Viewy::DependencyManagement::ViewRefresher.new(Viewy.connection)
  refresher.refresh_materialized_view(full_table_name, with_dependencies: true, concurrently: concurrently)
end
refresh_without_dependencies!(concurrently: false) click to toggle source

Refreshes this view without refreshing any dependencies

@raise [ActiveRecord::StatementInvalidError] raised if a dependent view is somehow not refreshed correctly

@return [PG::Result] the result of the refresh statement on the materialized view

# File lib/viewy/acts_as_materialized_view.rb, line 24
def refresh_without_dependencies!(concurrently: false)
  refresher = Viewy::DependencyManagement::ViewRefresher.new(Viewy.connection)
  refresher.refresh_materialized_view(full_table_name, with_dependencies: false, concurrently: concurrently)
end
schema_name() click to toggle source
# File lib/viewy/acts_as_materialized_view.rb, line 55
        def schema_name
  chunks = table_name.to_s.partition('.')
  if chunks[2].present?
    ''
  else
    "#{connection.current_schema}."
  end
end
sorted_view_dependencies() click to toggle source

Provides an array of sorted view dependencies

@return [Array<String>]

# File lib/viewy/acts_as_materialized_view.rb, line 32
def sorted_view_dependencies
  view_dep = Viewy::Models::MaterializedViewDependency.find(full_table_name)
  Viewy::DependencyManagement::ViewSorter.new.sorted_materialized_view_subset(view_names: view_dep.view_dependencies)
end