@@ -14,7 +14,7 @@ module Scenic
14
14
module Adapters
15
15
# An adapter for managing Postgres views.
16
16
#
17
- # These methods are used interally by Scenic and are not intended for direct
17
+ # These methods are used internally by Scenic and are not intended for direct
18
18
# use. Methods that alter database schema are intended to be called via
19
19
# {Statements}, while {#refresh_materialized_view} is called via
20
20
# {Scenic.database}.
@@ -124,7 +124,7 @@ def drop_view(name)
124
124
# @param sql_definition The SQL schema that defines the materialized view.
125
125
# @param no_data [Boolean] Default: false. Set to true to create
126
126
# materialized view without running the associated query. You will need
127
- # to perform a non-concurrent refresh to populate with data.
127
+ # to perform a refresh to populate with data.
128
128
#
129
129
# This is typically called in a migration via {Statements#create_view}.
130
130
#
@@ -154,7 +154,7 @@ def create_materialized_view(name, sql_definition, no_data: false)
154
154
# @param sql_definition The SQL schema for the updated view.
155
155
# @param no_data [Boolean] Default: false. Set to true to create
156
156
# materialized view without running the associated query. You will need
157
- # to perform a non-concurrent refresh to populate with data.
157
+ # to perform a refresh to populate with data.
158
158
#
159
159
# @raise [MaterializedViewsNotSupportedError] if the version of Postgres
160
160
# in use does not support materialized views.
@@ -193,7 +193,10 @@ def drop_materialized_view(name)
193
193
# refreshed without locking the view for select but requires that the
194
194
# table have at least one unique index that covers all rows. Attempts to
195
195
# refresh concurrently without a unique index will raise a descriptive
196
- # error.
196
+ # error. This option is ignored if the view is not populated, as it
197
+ # would cause an error to be raised by Postgres. Default: false.
198
+ # @param cascade [Boolean] Whether to refresh dependent materialized
199
+ # views. Default: false.
197
200
#
198
201
# @raise [MaterializedViewsNotSupportedError] if the version of Postgres
199
202
# in use does not support materialized views.
@@ -205,26 +208,29 @@ def drop_materialized_view(name)
205
208
# Scenic.database.refresh_materialized_view(:search_results)
206
209
# @example Concurrent refresh
207
210
# Scenic.database.refresh_materialized_view(:posts, concurrently: true)
211
+ # @example Cascade refresh
212
+ # Scenic.database.refresh_materialized_view(:posts, cascade: true)
208
213
#
209
214
# @return [void]
210
215
def refresh_materialized_view ( name , concurrently : false , cascade : false )
211
216
raise_unless_materialized_views_supported
212
217
218
+ if concurrently
219
+ raise_unless_concurrent_refresh_supported
220
+ end
221
+
213
222
if cascade
214
223
refresh_dependencies_for ( name , concurrently : concurrently )
215
224
end
216
225
217
- if concurrently
218
- raise_unless_concurrent_refresh_supported
226
+ if concurrently && populated? ( name )
219
227
execute "REFRESH MATERIALIZED VIEW CONCURRENTLY #{ quote_table_name ( name ) } ;"
220
228
else
221
229
execute "REFRESH MATERIALIZED VIEW #{ quote_table_name ( name ) } ;"
222
230
end
223
231
end
224
232
225
- # True if supplied relation name is populated. Useful for checking the
226
- # state of materialized views which may error if created `WITH NO DATA`
227
- # and used before they are refreshed. True for all other relation types.
233
+ # True if supplied relation name is populated.
228
234
#
229
235
# @param name The name of the relation
230
236
#
@@ -235,7 +241,7 @@ def refresh_materialized_view(name, concurrently: false, cascade: false)
235
241
def populated? ( name )
236
242
raise_unless_materialized_views_supported
237
243
238
- schemaless_name = name . split ( "." ) . last
244
+ schemaless_name = name . to_s . split ( "." ) . last
239
245
240
246
sql = "SELECT relispopulated FROM pg_class WHERE relname = '#{ schemaless_name } '"
241
247
relations = execute ( sql )
0 commit comments