Module: Timescaledb::Scenic::Extension

Defined in:
lib/timescaledb/scenic/extension.rb

Instance Method Summary collapse

Instance Method Details

#create_materialized_view(name, sql_definition, with: nil, no_data: false) ⇒ void

This method returns an undefined value.

Creates a materialized view in the database

This is typically called in a migration via Statements#create_view.

Parameters:

  • name

    The name of the materialized view to create

  • sql_definition

    The SQL schema that defines the materialized view.

  • with (String) (defaults to: nil)

    Default: nil. Set with: “…” to add “WITH (…)”.

  • no_data (Boolean) (defaults to: false)

    Default: false. Set to true to not create data. materialized view without running the associated query. You will need to perform a non-concurrent refresh to populate with data.



17
18
19
20
21
22
23
24
# File 'lib/timescaledb/scenic/extension.rb', line 17

def create_materialized_view(name, sql_definition, with: nil, no_data: false)
  execute <<-SQL
  CREATE MATERIALIZED VIEW #{quote_table_name(name)}
  #{"WITH (#{with})" if with} AS
  #{sql_definition.rstrip.chomp(';')}
  #{'WITH NO DATA' if no_data};
  SQL
end

#create_view(name, version: nil, with: nil, sql_definition: nil, materialized: false, no_data: false) ⇒ Object

to add the ‘with: ` keyword that can be used for such option.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/timescaledb/scenic/extension.rb', line 28

def create_view(name, version: nil, with: nil, sql_definition: nil, materialized: false, no_data: false)
  if version.present? && sql_definition.present?
    raise(
      ArgumentError,
      "sql_definition and version cannot both be set",
    )
  end

  if version.blank? && sql_definition.blank?
    version = 1
  end

  sql_definition ||= definition(name, version)

  if materialized
    ::Scenic.database.create_materialized_view(
      name,
      sql_definition,
      no_data: no_data,
      with: with
    )
  else
    ::Scenic.database.create_view(name, sql_definition, with: with)
  end
end