class PgMaterializedView

Attributes

name[R]

Public Class Methods

[](name) click to toggle source
# File lib/pg_materialized_view.rb, line 15
def self.[](name)
  name = name.to_s
  raise "not a materialized view: #{name}" unless names.include?(name)
  instances[name]
end
all() click to toggle source
# File lib/pg_materialized_view.rb, line 21
def self.all
  names.map { |name| instances[name] }
end
db() click to toggle source
# File lib/pg_materialized_view.rb, line 3
def self.db
  ActiveRecord::Base.connection
end
instances() click to toggle source
# File lib/pg_materialized_view.rb, line 7
def self.instances
  @instances ||= Hash.new { |h, name| h[name] = new(name) }
end
names() click to toggle source
# File lib/pg_materialized_view.rb, line 11
def self.names
  db.select_values("SELECT oid::regclass::text FROM pg_class WHERE relkind = 'm'")
end
new(name) click to toggle source
# File lib/pg_materialized_view.rb, line 31
def initialize(name)
  @name = name
end
refresh_all() click to toggle source
# File lib/pg_materialized_view.rb, line 25
def self.refresh_all
  all.each(&:refresh)
end

Public Instance Methods

refresh() click to toggle source

Public: refresh the materialized view, populates the view if needed

# File lib/pg_materialized_view.rb, line 36
def refresh
  begin
    self.class.db.execute("REFRESH MATERIALIZED VIEW CONCURRENTLY #{name}")
  rescue ActiveRecord::StatementInvalid => e
    raise e unless e.message =~ /CONCURRENTLY cannot be used when the materialized view is not populated/
    self.class.db.execute("REFRESH MATERIALIZED VIEW #{name}")
  end
end