class

Lustra::View

Inherits Reference < Object

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

apply(direction : Symbol, view_name : String, apply_cache : Set(String))

install the view into postgresql using CREATE VIEW

Source
apply(direction : Symbol, apply_cache = Set(String).new)

install the view into postgresql using CREATE VIEW

Source
register(name : Lustra::SQL::Symbolic, &)

Call the DSL to register a new view

Lustra::View.register(:name) do |view|
  # describe the view here.
end
Source

Instance methods

connection(connection : String)

database connection where is installed the view

Source
connection
Source
full_name
Source
materialized(mat : Bool)

whether the view is materialized or not. I would recommend to use migration execute create/drop whenever the view is a materialized view

Source
materialized?
Source
name(value : String | Symbol)

name of the view

Source
name
Source
query(query : String)

query body related to the view. Must be a SELECT clause

Source
query
Source
require(*req)

list of dependencies from the other view related to this view

Source
requirement
Source
schema(value : String | Symbol)

schema to store the view (default public)

Source
schema
Source
to_create_sql
Source
to_drop_sql
Source