class PGTrunk::Operations::CompositeTypes::Column

@private Definition for an column of a composite type

Constants

INVERTED

Public Class Methods

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

Public Instance Methods

inversion_error() click to toggle source
# File lib/pg_trunk/operations/composite_types/column.rb, line 58
    def inversion_error
      return <<~MSG.squish if if_exists
        with `if_exists: true` option cannot be inverted
        due to uncertainty of the previous state of the database.
      MSG

      return <<~MSG.squish if force == :cascade
        with `force: :cascade` option cannot be inverted
        due to uncertainty of the previous state of the database.
      MSG

      return <<~MSG.squish if change == :drop && type.blank?
        undefined type of the dropped column #{name}
      MSG

      return <<~MSG.squish if change == :alter && type && !from_type
        undefined a previous state of the type for column #{name}
      MSG

      return <<~MSG.squish if change == :alter && collation && !from_collation
        undefined a previous state of the collation for column #{name}
      MSG
    end
invert() click to toggle source
# File lib/pg_trunk/operations/composite_types/column.rb, line 38
def invert
  @invert ||= {}.tap do |i|
    i[:change] = INVERTED[change]
    i[:name] = new_name.presence || name
    i[:new_name] = name if new_name.present?
    i[:type] = change == :add ? type : from_type
    i[:collation] = change == :add ? collation : from_collation
  end
end
to_h() click to toggle source
# File lib/pg_trunk/operations/composite_types/column.rb, line 30
def to_h
  @to_h ||= attributes.compact.symbolize_keys
end
to_sql() click to toggle source
# File lib/pg_trunk/operations/composite_types/column.rb, line 48
def to_sql
  case change
  when :add    then add_sql
  when :alter  then alter_sql
  when :drop   then drop_sql
  when :rename then rename_sql
  else sql
  end
end

Private Instance Methods

add_sql() click to toggle source
# File lib/pg_trunk/operations/composite_types/column.rb, line 98
def add_sql
  "ADD ATTRIBUTE #{name.inspect} #{type.lean}".tap do |sql|
    sql << " COLLATE #{collation.to_sql}" if collation.present?
    sql << " CASCADE" if force == :cascade
  end
end
alter_sql() click to toggle source
# File lib/pg_trunk/operations/composite_types/column.rb, line 105
def alter_sql
  "ALTER ATTRIBUTE #{name.inspect} SET DATA TYPE #{type.lean}".tap do |sql|
    sql << " COLLATE #{collation.to_sql}" if collation.present?
    sql << " CASCADE" if force == :cascade
  end
end
drop_sql() click to toggle source
# File lib/pg_trunk/operations/composite_types/column.rb, line 90
def drop_sql
  "DROP ATTRIBUTE".tap do |sql|
    sql << " IF EXISTS" if if_exists
    sql << " #{name.inspect}"
    sql << " CASCADE" if force == :cascade
  end
end
rename_sql() click to toggle source
# File lib/pg_trunk/operations/composite_types/column.rb, line 84
def rename_sql
  "RENAME ATTRIBUTE #{name.inspect} TO #{new_name.inspect}".tap do |sql|
    sql << " CASCADE" if force == :cascade
  end
end
sql() click to toggle source
# File lib/pg_trunk/operations/composite_types/column.rb, line 112
def sql
  "#{name.inspect} #{type.lean}".tap do |sql|
    sql << " COLLATE #{collation.to_sql}" if collation
  end
end