class PGTrunk::Operations::CompositeTypes::ChangeCompositeType

@private

Public Instance Methods

add_column(name, type, collation: nil) click to toggle source

Methods to populate ‘columns` from the block

# File lib/pg_trunk/operations/composite_types/change_composite_type.rb, line 67
def add_column(name, type, collation: nil)
  columns << Column.new(
    name: name, type: type, collation: collation, change: :add, force: force,
  )
end
change_column(name, type, **opts) click to toggle source
# File lib/pg_trunk/operations/composite_types/change_composite_type.rb, line 80
def change_column(name, type, **opts)
  opts = opts.slice(:collation, :from_type, :from_collation)
  columns << Column.new(
    name: name, type: type, force: force, change: :alter, **opts,
  )
end
drop_column(name, type = nil, **opts) click to toggle source
# File lib/pg_trunk/operations/composite_types/change_composite_type.rb, line 73
def drop_column(name, type = nil, **opts)
  opts = opts.slice(:if_exists, :collation)
  columns << Column.new(
    name: name, type: type, force: force, **opts, change: :drop,
  )
end
invert() click to toggle source
# File lib/pg_trunk/operations/composite_types/change_composite_type.rb, line 100
def invert
  keys = inversion.select { |_, v| v.nil? }.keys.join(", ").presence
  errors = columns.map(&:inversion_error).compact
  errors << "Can't invert #{keys}" if keys.present?
  errors << "Can't invert dropped columns" if columns.any? { |c| c.change == :drop }
  raise IrreversibleMigration.new(self, nil, *errors) if errors.any?

  self.class.new(**to_h, **inversion)
end
rename_column(name, to:) click to toggle source
# File lib/pg_trunk/operations/composite_types/change_composite_type.rb, line 87
def rename_column(name, to:)
  columns << Column.new(
    name: name, new_name: to, force: force, change: :rename,
  )
end
to_sql(_version) click to toggle source
# File lib/pg_trunk/operations/composite_types/change_composite_type.rb, line 96
def to_sql(_version)
  [*change_columns, *rename_columns, *change_comment].join(" ")
end

Private Instance Methods

change() click to toggle source
# File lib/pg_trunk/operations/composite_types/change_composite_type.rb, line 131
def change
  @change ||= {
    comment: comment,
    columns: columns.select(&:change).map(&:to_h).presence,
  }.compact
end
change_columns() click to toggle source
# File lib/pg_trunk/operations/composite_types/change_composite_type.rb, line 112
def change_columns
  list = columns.select { |c| c.change&.!= :rename }
  return if list.blank?

  "ALTER TYPE #{name.to_sql} #{list.map(&:to_sql).join(', ')};"
end
change_comment() click to toggle source
# File lib/pg_trunk/operations/composite_types/change_composite_type.rb, line 125
    def change_comment
      <<~SQL.squish if comment
        COMMENT ON TYPE #{name.to_sql} IS $comment$#{comment}$comment$;
      SQL
    end
inversion() click to toggle source
# File lib/pg_trunk/operations/composite_types/change_composite_type.rb, line 138
def inversion
  @inversion ||= {
    comment: from_comment,
    columns: columns.reverse.map(&:invert).presence,
  }.slice(*change.keys)
end
rename_columns() click to toggle source
# File lib/pg_trunk/operations/composite_types/change_composite_type.rb, line 119
def rename_columns
  columns.select { |c| c.change == :rename }.map do |c|
    "ALTER TYPE #{name.to_sql} #{c.to_sql};"
  end
end