activerecord_views 0.0.7 → 0.0.8

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: b5c0f7297c9900d6d2ce6e803a35eca6a8015c84
4
- data.tar.gz: 5a5874b77eec4404e21b2a2ac07bc9a78e2ff0cd
3
+ metadata.gz: 373526856a79d7a2d2c97de39373d0a9a791e70c
4
+ data.tar.gz: 942cecdc3e31f8417166778362f19567ce16774f
5
5
  SHA512:
6
- metadata.gz: f58e18bb2e9ae78e98b127074038856bf4a94273d482022d5ab38fc5f7bac2be9e1deb0f989ba32c9040019008c1dcf49247ef7a4c5de56ae5b2b2283d9df584
7
- data.tar.gz: 8d38a7027e201afa5f6884792b4da2e2957400d2b0908525d838c29cf03d019c0f02646f97fed8e0b88b94cd3420b617881ee1c5b187c1cdbea399198b45ca8e
6
+ metadata.gz: 18d20a0204d37764802b32ef58fd532f8c39cd6ad82b786eb55c4b2d3bd298fa67a84afac9bedb3cb6ca58fd45e280bac0d3852f11d3f92f53625a5f4f4fe653
7
+ data.tar.gz: 68d3db12c7ea7c8cdaafb1391efab4a9f5a59ad579069ca8246ed47abf1c028b48ffa41b16a2eadfbfacfbe7194e10a9cc9222f5c01def2576c39830e708e69f
data/README.markdown CHANGED
@@ -102,10 +102,10 @@ end
102
102
 
103
103
  Note: If your view has a single column as the unique key, you can also tell ActiveRecord about it by adding `self.primary_key = :account_id` in your model file. This is required for features such as `.find` and `.find_each` to work.
104
104
 
105
- Once you have defined the unique columns for the view, you can then use `concurrent: true` when refreshing:
105
+ Once you have defined the unique columns for the view, you can then use `concurrent: true` to force a concurrent refresh or `concurrent: :auto` to concurrently refresh when possible:
106
106
 
107
107
  ```ruby
108
- AccountBalance.refresh_view! concurrent: true
108
+ AccountBalance.refresh_view! concurrent: :auto
109
109
  ```
110
110
 
111
111
  ## Pre-populating views in Rails development mode
@@ -36,7 +36,19 @@ module ActiveRecordViews
36
36
 
37
37
  def refresh_view!(options = {})
38
38
  options.assert_valid_keys :concurrent
39
- connection.execute "REFRESH MATERIALIZED VIEW#{' CONCURRENTLY' if options[:concurrent]} #{connection.quote_table_name self.table_name};"
39
+
40
+ concurrent = case options[:concurrent]
41
+ when nil, false
42
+ false
43
+ when true
44
+ true
45
+ when :auto
46
+ view_populated? && ActiveRecordViews.supports_concurrent_refresh?(connection)
47
+ else
48
+ raise ArgumentError, 'invalid concurrent option'
49
+ end
50
+
51
+ connection.execute "REFRESH MATERIALIZED VIEW#{' CONCURRENTLY' if concurrent} #{connection.quote_table_name self.table_name};"
40
52
  end
41
53
 
42
54
  def view_populated?
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordViews
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
3
3
  end
@@ -185,9 +185,13 @@ module ActiveRecordViews
185
185
  end
186
186
 
187
187
  dependencies = get_view_dependencies(connection, name)
188
+ cache = ActiveRecordViews::ChecksumCache.new(connection)
189
+ dependency_metadata = {}
188
190
 
189
191
  dependencies.reverse.each do |dependency_name, _, _, _|
190
192
  execute_drop_view connection, dependency_name
193
+ dependency_metadata[dependency_name] = cache.get(dependency_name)
194
+ cache.set dependency_name, nil
191
195
  end
192
196
 
193
197
  yield
@@ -199,6 +203,7 @@ module ActiveRecordViews
199
203
  raise unless e.missing_name?(class_name)
200
204
  options = JSON.load(options_json).symbolize_keys
201
205
  execute_create_view connection, dependency_name, definition, options
206
+ cache.set dependency_name, dependency_metadata[dependency_name]
202
207
  end
203
208
  end
204
209
  end
@@ -97,6 +97,14 @@ describe ActiveRecordViews::Extension do
97
97
  expect(ModifiedB.first.attributes.except(nil)).to eq('new_name' => 22)
98
98
  end
99
99
 
100
+ it 'successfully restores dependant view when temporarily dropping dependency' do
101
+ ActiveRecordViews.create_view ActiveRecord::Base.connection, 'dependency_as', 'DependencyA', 'SELECT 42 AS foo, 1 AS id;'
102
+ ActiveRecordViews.create_view ActiveRecord::Base.connection, 'dependency_bs', 'DependencyB', 'SELECT * FROM dependency_as;'
103
+
104
+ expect(DependencyA.first.id).to eq 2
105
+ expect(DependencyB.first.id).to eq 2
106
+ end
107
+
100
108
  it 'errors if more than one argument is specified' do
101
109
  expect {
102
110
  class TooManyArguments < ActiveRecord::Base
@@ -166,5 +174,27 @@ describe ActiveRecordViews::Extension do
166
174
  MaterializedViewRefreshTestModel.refresh_view!
167
175
  MaterializedViewConcurrentRefreshTestModel.refresh_view! concurrent: true
168
176
  end
177
+
178
+ it 'supports opportunistically refreshing materialized views concurrently' do
179
+ class MaterializedViewAutoRefreshTestModel < ActiveRecord::Base
180
+ is_view 'SELECT 1 AS id;', materialized: true, unique_columns: [:id]
181
+ end
182
+
183
+ expect(ActiveRecord::Base.connection).to receive(:execute).with('REFRESH MATERIALIZED VIEW "materialized_view_auto_refresh_test_models";').once.and_call_original
184
+ expect(ActiveRecord::Base.connection).to receive(:execute).with('REFRESH MATERIALIZED VIEW CONCURRENTLY "materialized_view_auto_refresh_test_models";').once.and_call_original
185
+
186
+ MaterializedViewAutoRefreshTestModel.refresh_view! concurrent: :auto
187
+ MaterializedViewAutoRefreshTestModel.refresh_view! concurrent: :auto
188
+ end
189
+
190
+ it 'raises an error when refreshing materialized views with invalid concurrent option' do
191
+ class MaterializedViewInvalidRefreshTestModel < ActiveRecord::Base
192
+ is_view 'SELECT 1 AS id;', materialized: true, unique_columns: [:id]
193
+ end
194
+
195
+ expect {
196
+ MaterializedViewAutoRefreshTestModel.refresh_view! concurrent: :blah
197
+ }.to raise_error ArgumentError, 'invalid concurrent option'
198
+ end
169
199
  end
170
200
  end
@@ -0,0 +1,3 @@
1
+ class DependencyA < ActiveRecord::Base
2
+ is_view 'SELECT 2 AS id;'
3
+ end
@@ -0,0 +1,3 @@
1
+ class DependencyB < ActiveRecord::Base
2
+ is_view 'SELECT * FROM dependency_as;'
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_views
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Weathered
@@ -115,6 +115,8 @@ files:
115
115
  - spec/active_record_views_checksum_cache_spec.rb
116
116
  - spec/active_record_views_extension_spec.rb
117
117
  - spec/active_record_views_spec.rb
118
+ - spec/internal/app/models/dependency_a.rb
119
+ - spec/internal/app/models/dependency_b.rb
118
120
  - spec/internal/app/models/erb_test_model.rb
119
121
  - spec/internal/app/models/erb_test_model.sql.erb
120
122
  - spec/internal/app/models/external_file_test_model.rb
@@ -156,6 +158,8 @@ test_files:
156
158
  - spec/active_record_views_checksum_cache_spec.rb
157
159
  - spec/active_record_views_extension_spec.rb
158
160
  - spec/active_record_views_spec.rb
161
+ - spec/internal/app/models/dependency_a.rb
162
+ - spec/internal/app/models/dependency_b.rb
159
163
  - spec/internal/app/models/erb_test_model.rb
160
164
  - spec/internal/app/models/erb_test_model.sql.erb
161
165
  - spec/internal/app/models/external_file_test_model.rb