class PGTrunk::Operations::Enums::ChangeEnum

@private

Public Instance Methods

add_value(name, after: nil, before: nil) click to toggle source

Add new value (irreversible!) If neither option is specified, the value will be added to the very end of the array. Notice, that all add-ons are made BEFORE renames.

# File lib/pg_trunk/operations/enums/change_enum.rb, line 48
def add_value(name, after: nil, before: nil)
  changes << Change.new(name: name, after: after, before: before)
end
invert() click to toggle source
# File lib/pg_trunk/operations/enums/change_enum.rb, line 73
    def invert
      values_added = changes.any?(&:add?)
      raise IrreversibleMigration.new(self, nil, <<~MSG.squish) if values_added
        Removal of values from enumerated type is not supported by PostgreSQL,
        that's why adding new values can't be reverted.
      MSG

      undefined = inversion.select { |_, v| v.nil? }.keys.join(", ").presence
      raise IrreversibleMigration.new(self, nil, <<~MSG.squish) if undefined
        Undefined values to revert #{undefined}.
      MSG

      self.class.new(**to_h, **inversion)
    end
rename_value(name, to: nil) click to toggle source

Rename the value to new unique name (reversible)

# File lib/pg_trunk/operations/enums/change_enum.rb, line 53
def rename_value(name, to: nil)
  changes << Change.new(name: name, new_name: to)
end
to_h() click to toggle source
Calls superclass method PGTrunk::Operation::Attributes#to_h
# File lib/pg_trunk/operations/enums/change_enum.rb, line 88
def to_h
  super.tap { |data| data[:changes]&.map!(&:to_h) }
end
to_sql(version) click to toggle source
# File lib/pg_trunk/operations/enums/change_enum.rb, line 64
    def to_sql(version)
      raise <<~MSG.squish if version < "12" && changes.any?(&:add?)
        Adding new values to enumerable types inside a migration
        is supported in PostgreSQL v12+.
      MSG

      [*add_values, *rename_values, *change_comment].join(" ")
    end

Private Instance Methods

add_values() click to toggle source
# File lib/pg_trunk/operations/enums/change_enum.rb, line 94
def add_values
  changes.select(&:add?).map do |change|
    "ALTER TYPE #{name.to_sql} #{change.to_sql};"
  end
end
change() click to toggle source
# File lib/pg_trunk/operations/enums/change_enum.rb, line 112
def change
  @change ||= {
    changes: changes.map(&:to_h).presence, comment: comment,
  }.compact
end
change_comment() click to toggle source
# File lib/pg_trunk/operations/enums/change_enum.rb, line 106
def change_comment
  return unless comment # empty string is processed

  "COMMENT ON TYPE #{name.to_sql} IS $comment$#{comment}$comment$;"
end
inversion() click to toggle source
# File lib/pg_trunk/operations/enums/change_enum.rb, line 118
def inversion
  @inversion ||= {
    changes: changes.reverse.map(&:invert).presence, comment: from_comment,
  }.slice(*change.keys)
end
rename_values() click to toggle source
# File lib/pg_trunk/operations/enums/change_enum.rb, line 100
def rename_values
  changes.select(&:rename?).map do |change|
    "ALTER TYPE #{name.to_sql} #{change.to_sql};"
  end
end