Class: ActiveRecord::ConnectionAdapters::PostgreSQLViewAlterer

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

Constant Summary collapse

VALID_OPTIONS =
%w{
  set_default
  drop_default
  owner_to
  rename_to
  set_schema
  set_options
  reset_options
}.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 = {}) ⇒ PostgreSQLViewAlterer

:nodoc:



174
175
176
# File 'lib/active_record/postgresql_extensions/views.rb', line 174

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.



162
163
164
# File 'lib/active_record/postgresql_extensions/views.rb', line 162

def actions
  @actions
end

#baseObject

Returns the value of attribute base.



162
163
164
# File 'lib/active_record/postgresql_extensions/views.rb', line 162

def base
  @base
end

#nameObject

Returns the value of attribute name.



162
163
164
# File 'lib/active_record/postgresql_extensions/views.rb', line 162

def name
  @name
end

#optionsObject

Returns the value of attribute options.



162
163
164
# File 'lib/active_record/postgresql_extensions/views.rb', line 162

def options
  @options
end

Instance Method Details

#to_sqlObject

:nodoc:



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/active_record/postgresql_extensions/views.rb', line 178

def to_sql #:nodoc:
  all_sql = []

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

    if actions.key?(key)
      sql = "ALTER VIEW "

      if options.key?(:if_exists)
        ActiveRecord::PostgreSQLExtensions::Features.check_feature(:view_if_exists)

        sql << "IF EXISTS " if options[:if_exists]
      end

      sql << "#{base.quote_view_name(name)} "

      sql << case key
        when :set_default
          column, expression = actions[:set_default].flatten
          "ALTER COLUMN #{base.quote_column_name(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(", ") << ')'
      end

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

  all_sql.join("\n")
end