Class: PGTrunk::Operations::CompositeTypes::ChangeCompositeType

Inherits:
Base show all
Defined in:
lib/pg_trunk/operations/composite_types/change_composite_type.rb

Instance Method Summary collapse

Methods inherited from Base

#column

Methods included from PGTrunk::Operation::SQLHelpers

#quote

Methods included from PGTrunk::Operation::RubyHelpers

#dump, #to_a, #to_opts, #to_ruby

Methods included from PGTrunk::Operation::Inversion

#invert!, #irreversible!

Methods included from PGTrunk::Operation::Validations

#error_messages

Methods included from PGTrunk::Operation::Attributes

#attributes, #initialize

Instance Method Details

#add_column(name, type, collation: nil) ⇒ Object

Methods to populate columns from the block



67
68
69
70
71
# 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) ⇒ Object



80
81
82
83
84
85
# 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) ⇒ Object



73
74
75
76
77
78
# 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

#invertObject



100
101
102
103
104
105
106
107
108
# 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:) ⇒ Object



87
88
89
90
91
# 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) ⇒ Object



96
97
98
# 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