Class: PGTrunk::Operations::CompositeTypes::Column

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Validations
Defined in:
lib/pg_trunk/operations/composite_types/column.rb

Overview

Definition for an column of a composite type

Constant Summary collapse

INVERTED =
{
  add: :drop, drop: :add, rename: :rename, alter: :alter,
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(data) ⇒ Object



11
12
13
# File 'lib/pg_trunk/operations/composite_types/column.rb', line 11

def self.build(data)
  data.is_a?(self) ? data : new(**data)
end

Instance Method Details

#inversion_errorObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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

#invertObject



38
39
40
41
42
43
44
45
46
# 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_hObject



30
31
32
# File 'lib/pg_trunk/operations/composite_types/column.rb', line 30

def to_h
  @to_h ||= attributes.compact.symbolize_keys
end

#to_sqlObject



48
49
50
51
52
53
54
55
56
# 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