Lustra::View
Create and maintain your database views directly in the code.
You could use the migration system to create and drop your views. However this is proven to be difficult, even more if you want to update a view which depends on another subviews.
How it works ?
When you migrate using the migration system, all the views registered are going to be destroyed and recreated again. Order of creation depends of the requirement for each views
Example
Lustra::View.register :room_per_days do |view|
view.require(:rooms, :year_days)
view.query <<-SQL
SELECT room_id, day
FROM year_days
CROSS JOIN rooms
SQL
end
Lustra::View.register :rooms do |view|
view.query <<-SQL
SELECT room.id AS room_id
FROM generate_series(1, 4) AS room(id)
SQL
end
Lustra::View.register :year_days do |view|
view.query <<-SQL
SELECT date.day::date AS day
FROM generate_series(
date_trunc('day', NOW()),
date_trunc('day', NOW() + INTERVAL '364 days'),
INTERVAL '1 day'
) AS date(day)
SQL
end
In the example above, room_per_days will be first dropped before a migration start and last created after the migration finished, to prevent issue where some views are linked to others
Class methods
install the view into postgresql using CREATE VIEW
install the view into postgresql using CREATE VIEW
Call the DSL to register a new view
Lustra::View.register(:name) do |view|
# describe the view here.
end
Instance methods
whether the view is materialized or not. I would recommend to use migration execute create/drop whenever the view is a materialized view