Module: SchemaPlus::Views::ActiveRecord::ConnectionAdapters::PostgresqlAdapter

Defined in:
lib/schema_plus/views/active_record/connection_adapters/postgresql_adapter.rb

Constant Summary collapse

POSTGIS_VIEWS =
%W[
  geography_columns
  geometry_columns
  raster_columns
  raster_overviews
].freeze

Instance Method Summary collapse

Instance Method Details

#create_view(view_name, definition, options = {}) ⇒ Object

Create a view given the SQL definition. Specify :force => true to first drop the view if it already exists.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/schema_plus/views/active_record/connection_adapters/postgresql_adapter.rb', line 14

def create_view(view_name, definition, options={})
  SchemaMonkey::Middleware::Migration::CreateView.start(connection: self, view_name: view_name, definition: definition, options: options) do |env|
    definition = env.definition
    view_name = env.view_name
    options = env.options
    definition = definition.to_sql if definition.respond_to? :to_sql

    if options[:materialized] && options[:allow_replace]
      raise ArgumentError, 'allow_replace is not supported for materialized views'
    end

    if options[:force]
      drop_view(view_name, {if_exists: true}.merge(options.slice(:materialized)))
    end

    command = if options[:materialized]
                "CREATE MATERIALIZED"
              elsif options[:allow_replace]
                "CREATE OR REPLACE"
              else
                "CREATE"
              end

    execute "#{command} VIEW #{quote_table_name(view_name)} AS #{definition}"
  end
end

#drop_view(view_name, options = {}) ⇒ Object

Drop the named view. Specify :if_exists => true to fail silently if the view doesn’t exist.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/schema_plus/views/active_record/connection_adapters/postgresql_adapter.rb', line 43

def drop_view(view_name, options = {})
  SchemaMonkey::Middleware::Migration::DropView.start(connection: self, view_name: view_name, options: options) do |env|
    view_name = env.view_name
    options = env.options
    materialized = options[:materialized] ? 'MATERIALIZED' : ''
    sql = "DROP #{materialized} VIEW"
    sql += " IF EXISTS" if options[:if_exists]
    sql += " #{quote_table_name(view_name)}"
    execute sql
  end
end

#refresh_view(view_name, options = {}) ⇒ Object

Refresh a materialized view.



56
57
58
59
60
61
62
# File 'lib/schema_plus/views/active_record/connection_adapters/postgresql_adapter.rb', line 56

def refresh_view(view_name, options = {})
  SchemaMonkey::Middleware::Migration::RefreshView.start(connection: self, view_name: view_name, options: options) do |env|
    view_name = env.view_name
    sql = "REFRESH MATERIALIZED VIEW #{quote_table_name(view_name)}"
    execute sql
  end
end

#view_full_definition(view_name, name = nil) ⇒ Object

:nodoc:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/schema_plus/views/active_record/connection_adapters/postgresql_adapter.rb', line 71

def view_full_definition(view_name, name = nil) #:nodoc:
  data = SchemaMonkey::Middleware::Schema::ViewDefinition.start(connection: self, view_name: view_name, query_name: name, view_type: :view) { |env|
      result = env.connection.query(<<-SQL, name)
        SELECT pg_get_viewdef(oid), relkind
          FROM pg_class
        WHERE relkind in ('v', 'm')
          AND relname = '#{env.view_name}'
      SQL
      row = result.first
      unless row.nil?
        env.definition = row.first.chomp(';').strip
        env.view_type = :materialized if row.second == 'm'
      end
  }

  [data.definition, data.view_type]
end

#viewsObject

:nodoc:



64
65
66
67
68
69
# File 'lib/schema_plus/views/active_record/connection_adapters/postgresql_adapter.rb', line 64

def views #:nodoc:
  # Filter out any view that begins with "pg_"
  super.reject do |c|
    c.start_with?("pg_") || POSTGIS_VIEWS.include?(c)
  end
end