class PGTrunk::Operations::MaterializedViews::Column

@private Definition for the column change

Public Class Methods

build(data) click to toggle source
# File lib/pg_trunk/operations/materialized_views/column.rb, line 11
def self.build(data)
  data.is_a?(self) ? data : new(**data)
end

Public Instance Methods

changes() click to toggle source
# File lib/pg_trunk/operations/materialized_views/column.rb, line 36
def changes
  opts.except(:new_name)
end
error_messages() click to toggle source
# File lib/pg_trunk/operations/materialized_views/column.rb, line 71
def error_messages
  validate
  errors&.messages&.flat_map do |k, v|
    v.map do |msg|
      "Column #{name.inspect}: #{k == :base ? msg : "#{k} #{msg}"}"
    end
  end
end
invert() click to toggle source
# File lib/pg_trunk/operations/materialized_views/column.rb, line 40
def invert
  return { name: new_name, new_name: name } if new_name.present?

  {
    name: name,
    storage: (from_storage || :UNDEFINED if storage.present?),
    statistics: (0 if statistics.present?),
    n_distinct: (0 if n_distinct.present?),
  }.compact
end
opts() click to toggle source
# File lib/pg_trunk/operations/materialized_views/column.rb, line 32
def opts
  to_h.except(:name)
end
to_h() click to toggle source

Hashify definitions

# File lib/pg_trunk/operations/materialized_views/column.rb, line 24
def to_h
  @to_h ||=
    attributes
    .symbolize_keys
    .transform_values(&:presence)
    .compact
end
to_sql(_version = "10") click to toggle source

Build SQL snippets for the column definition @return [Array<String>]

# File lib/pg_trunk/operations/materialized_views/column.rb, line 82
def to_sql(_version = "10")
  return ["RENAME COLUMN #{name.inspect} TO #{new_name.inspect}"] if new_name

  alter = "ALTER COLUMN #{name.inspect}"
  [
    *("#{alter} SET STATISTICS #{statistics}" if statistics),
    *("#{alter} SET (n_distinct = #{n_distinct})" if n_distinct),
    *("#{alter} RESET (n_distinct)" if n_distinct&.zero?),
    *("#{alter} SET STORAGE #{storage.to_s.upcase}" if storage.present?),
  ]
end