As of writing, no UI is provided; all operations are via service objects, background jobs, and rake tasks.
Define a materialized view
Definition is managed via the MatViews::MatViewDefinition
model. You can create definitions in Rails console, seeds, or migrations.
# create a new definition
definition = MatViews::MatViewDefinition.create!(
name: 'my_mat_view',
sql: <<-SQL.squish,
SELECT id, name, created_at
FROM users
WHERE active = true
SQL
refresh_strategy: :concurrent, # :regular (default), :concurrent (needs unique index), :swap
unique_index_columns: ['id'], # required for :concurrent and :swap strategies
dependencies: [] # optional, array of other MatViewDefinition names
)
After defining, you can create, refresh, or delete the materialized view using the provided services, background jobs, or rake tasks as described in the Manage Materialized Views section.
Update a materialized view definition
You can update an existing materialized view definition by modifying its attributes and saving the changes. For example:
definition = MatViews::MatViewDefinition.find_by(name: 'my_mat_view')
definition.update!(
sql: <<-SQL.squish,
SELECT id, name, created_at, email
FROM users
WHERE active = true AND email IS NOT NULL
SQL
)
After updating the definition, you may want to refresh the materialized view to apply the changes, using one of the methods described in the Manage Materialized Views section.
Delete a materialized view definition
To delete a materialized view definition, you can use the destroy
method on the MatViews::MatViewDefinition
instance. For example:
Stop! Before deleting, ensure that you also drop the actual materialized view from the database if it exists, using the services or rake tasks provided, as deleting the definition does not automatically drop the view. See the Manage Materialized Views section for details.
definition = MatViews::MatViewDefinition.find_by(name: 'my_mat_view')
definition.destroy