Class: ActiveRecord::ConnectionAdapters::PostgreSQLMaterializedViewAlterer

Inherits:
Object
  • Object
show all
Includes:
PostgreSQLExtensions::Utils
Defined in:
lib/active_record/postgresql_extensions/materialized_views.rb

Overview

Alters a PostgreSQL materialized view definition. This class isn’t really meant to be used directly. Instead, see the various PostgreSQLAdapter materialied views methods for usage.

Constant Summary collapse

VALID_OPTIONS =
%w{
  set_default
  drop_default
  owner_to
  rename_to
  set_schema
  set_options
  reset_options
  cluster_on
  remove_cluster
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PostgreSQLExtensions::Utils

#hash_or_array_of_hashes, #options_from_hash_or_string, #strip_heredoc

Constructor Details

#initialize(base, name, actions, options = {}) ⇒ PostgreSQLMaterializedViewAlterer

:nodoc:



206
207
208
# File 'lib/active_record/postgresql_extensions/materialized_views.rb', line 206

def initialize(base, name, actions, options = {}) #:nodoc:
  @base, @name, @actions, @options = base, name, actions, options
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



192
193
194
# File 'lib/active_record/postgresql_extensions/materialized_views.rb', line 192

def actions
  @actions
end

#baseObject

Returns the value of attribute base.



192
193
194
# File 'lib/active_record/postgresql_extensions/materialized_views.rb', line 192

def base
  @base
end

#nameObject

Returns the value of attribute name.



192
193
194
# File 'lib/active_record/postgresql_extensions/materialized_views.rb', line 192

def name
  @name
end

#optionsObject

Returns the value of attribute options.



192
193
194
# File 'lib/active_record/postgresql_extensions/materialized_views.rb', line 192

def options
  @options
end

Instance Method Details

#to_sqlObject

:nodoc:



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/active_record/postgresql_extensions/materialized_views.rb', line 210

def to_sql #:nodoc:
  all_sql = []

  VALID_OPTIONS.each do |key|
    key = key.to_sym

    if actions.key?(key)
      sql = "ALTER MATERIALIZED VIEW "
      sql << "IF EXISTS " if options[:if_exists]
      sql << "#{base.quote_view_name(name)} "

      sql << case key
        when :set_default
          expression = if actions[:set_default].is_a?(Hash) && actions[:set_default].key?(:expression)
             actions[:set_default][:expression]
          else
            base.quote(actions[:set_default])
          end

          "ALTER COLUMN #{base.quote_column_name(actions[:column])} SET DEFAULT #{expression}"

        when :drop_default
          "ALTER COLUMN #{base.quote_column_name(actions[:drop_default])} DROP DEFAULT"

        when :owner_to
          "OWNER TO #{base.quote_role(actions[:owner_to])}"

        when :rename_to
          "RENAME TO #{base.quote_generic_ignore_scoped_schema(actions[:rename_to])}"

        when :set_schema
          "SET SCHEMA #{base.quote_schema(actions[:set_schema])}"

        when :set_options
          ActiveRecord::PostgreSQLExtensions::Features.check_feature(:view_set_options)

          "SET (#{options_from_hash_or_string(actions[:set_options])})" if actions[:set_options].present?

        when :reset_options
          ActiveRecord::PostgreSQLExtensions::Features.check_feature(:view_set_options)

          'RESET (' << Array.wrap(actions[:reset_options]).collect { |value|
            base.quote_generic(value)
          }.join(", ") << ')'

        when :cluster_on
          "CLUSTER ON #{base.quote_generic(actions[:cluster_on])}"

        when :remove_cluster
          next unless actions[:remove_cluster]

          "SET WITHOUT CLUSTER"
      end

      all_sql << "#{sql};"
    end
  end

  all_sql.join("\n")
end