Class: PgMaterializedView
- Inherits:
-
Object
- Object
- PgMaterializedView
- Defined in:
- lib/pg_materialized_view.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .[](name) ⇒ Object
- .all ⇒ Object
- .db ⇒ Object
- .instances ⇒ Object
- .names ⇒ Object
- .refresh_all ⇒ Object
Instance Method Summary collapse
-
#initialize(name) ⇒ PgMaterializedView
constructor
A new instance of PgMaterializedView.
-
#refresh ⇒ Object
Public: refresh the materialized view, populates the view if needed.
Constructor Details
#initialize(name) ⇒ PgMaterializedView
Returns a new instance of PgMaterializedView.
31 32 33 |
# File 'lib/pg_materialized_view.rb', line 31 def initialize(name) @name = name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
29 30 31 |
# File 'lib/pg_materialized_view.rb', line 29 def name @name end |
Class Method Details
.[](name) ⇒ Object
15 16 17 18 19 |
# 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 ⇒ Object
21 22 23 |
# File 'lib/pg_materialized_view.rb', line 21 def self.all names.map { |name| instances[name] } end |
.db ⇒ Object
3 4 5 |
# File 'lib/pg_materialized_view.rb', line 3 def self.db ActiveRecord::Base.connection end |
.instances ⇒ Object
7 8 9 |
# File 'lib/pg_materialized_view.rb', line 7 def self.instances @instances ||= Hash.new { |h, name| h[name] = new(name) } end |
.names ⇒ Object
11 12 13 |
# 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 |
.refresh_all ⇒ Object
25 26 27 |
# File 'lib/pg_materialized_view.rb', line 25 def self.refresh_all all.each(&:refresh) end |
Instance Method Details
#refresh ⇒ Object
Public: refresh the materialized view, populates the view if needed
36 37 38 39 40 41 42 43 |
# 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. =~ /CONCURRENTLY cannot be used when the materialized view is not populated/ self.class.db.execute("REFRESH MATERIALIZED VIEW #{name}") end end |