activerecord_views 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f3c9db1b593bf137570cde8f2e4fcaf6837aa414
4
- data.tar.gz: 81143c5d6e77cfe47971b9bca74dde2f8a1c3847
3
+ metadata.gz: 39b0e9c321cfad9ac7d28ee1e273612407b088a8
4
+ data.tar.gz: 83c737c0270414db041cbbd0de28ec43bbec3da8
5
5
  SHA512:
6
- metadata.gz: fcf258e4d370ee70ad3a96488271d206ec7e8fe27dce4c123b9a176a10d12cd06cf2c1f3d7f2c21446a98045015967cbc4130d8414e48db0b9d1081e8afb6691
7
- data.tar.gz: 684a7373b1167ab08a20f565e7f33c93cc5420e85ec34186488af74f36772672927dcaa993198273d83f568fd05ffae05eaf2c8a40d55096a80af1ea9785ac8d
6
+ metadata.gz: a2ab26da9f69ebf4b37ab0d10a92ee685f7fa577fb089a3d038776b2073d4466108e959e5c232af30b7499048c010a15d283c3b68b5f0504e3e2e635b4eb431b
7
+ data.tar.gz: 583a160996c2db6067d425fa6a06505a9e6a484ded3d7b5f104c93f0775c25253adce5514d8f2cd57cd8c2b0405d83fa927c7a358dca1da00aa2f8c61a0a3f85
data/README.markdown CHANGED
@@ -123,6 +123,8 @@ AccountBalance.view_populated? # => true
123
123
 
124
124
  ActiveRecordViews records when a view was last refreshed. This is often useful for giving users an idea of how stale data. To retrieve this timestamp, call `.refreshed_at` on the model:
125
125
 
126
+ ActiveRecordViews also has a convenience method called `ensure_populated!` which checks all materialized views in a chain of dependencies have been initially populated. This can be used as a safeguard to lazily populate views on demand. You will probably also setup a schedule to periodically refresh the view data when it gets stale.
127
+
126
128
  PostgreSQL 9.4 supports refreshing materialized views concurrently. This allows other processes to continue reading old cached data while the view is being updated. To use this feature you must have define a unique index on the materialized view:
127
129
 
128
130
  ```sql
@@ -79,6 +79,17 @@ module ActiveRecordViews
79
79
  ActiveSupport::TimeZone['UTC'].parse(value)
80
80
  end
81
81
  end
82
+
83
+ def ensure_populated!
84
+ ActiveRecordViews.get_view_direct_dependencies(self.connection, self.table_name).each do |class_name|
85
+ klass = class_name.constantize
86
+ klass.ensure_populated!
87
+ end
88
+
89
+ if ActiveRecordViews.materialized_view?(self.connection, self.table_name)
90
+ refresh_view! unless view_populated?
91
+ end
92
+ end
82
93
  end
83
94
  end
84
95
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordViews
2
- VERSION = '0.0.13'
2
+ VERSION = '0.0.14'
3
3
  end
@@ -179,6 +179,29 @@ describe ActiveRecordViews::Extension do
179
179
  }.to raise_error ArgumentError, 'not a materialized view'
180
180
  end
181
181
 
182
+ it 'supports ensuring a view hierarchy has been populated' do
183
+ class EnsurePopulatedFoo < ActiveRecord::Base
184
+ is_view 'SELECT 1 AS id;', materialized: true
185
+ end
186
+
187
+ class EnsurePopulatedBar < ActiveRecord::Base
188
+ is_view "SELECT * FROM #{EnsurePopulatedFoo.table_name}", dependencies: [EnsurePopulatedFoo]
189
+ end
190
+
191
+ class EnsurePopulatedBaz < ActiveRecord::Base
192
+ is_view "SELECT * FROM #{EnsurePopulatedBar.table_name}", dependencies: [EnsurePopulatedBar]
193
+ end
194
+
195
+ expect(ActiveRecord::Base.connection).to receive(:execute).with(/^REFRESH MATERIALIZED VIEW/).once.and_call_original
196
+ allow(ActiveRecord::Base.connection).to receive(:execute).and_call_original
197
+
198
+ expect(EnsurePopulatedFoo.view_populated?).to eq false
199
+ EnsurePopulatedBaz.ensure_populated!
200
+ expect(EnsurePopulatedFoo.view_populated?).to eq true
201
+ EnsurePopulatedBaz.ensure_populated!
202
+ expect(EnsurePopulatedFoo.view_populated?).to eq true
203
+ end
204
+
182
205
  it 'supports refreshing materialized views concurrently' do
183
206
  class MaterializedViewRefreshTestModel < ActiveRecord::Base
184
207
  is_view 'SELECT 1 AS id;', materialized: true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_views
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Weathered
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-25 00:00:00.000000000 Z
11
+ date: 2015-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord