Class: PGTrunk::Operations::Domains::ChangeDomain

Inherits:
Base show all
Defined in:
lib/pg_trunk/operations/domains/change_domain.rb

Instance Method Summary collapse

Methods inherited from Base

#constraint

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_constraint(check, name: nil, valid: true) ⇒ Object

Methods to populate constraints from the block



62
63
64
# File 'lib/pg_trunk/operations/domains/change_domain.rb', line 62

def add_constraint(check, name: nil, valid: true)
  constraints << Constraint.new(name: name, check: check, valid: valid)
end

#drop_constraint(name, check: nil, if_exists: nil) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/pg_trunk/operations/domains/change_domain.rb', line 74

def drop_constraint(name, check: nil, if_exists: nil)
  constraints << Constraint.new(
    check: check,
    drop: true,
    force: force,
    if_exists: if_exists,
    name: name,
  )
end

#invertObject



92
93
94
95
96
97
98
99
# File 'lib/pg_trunk/operations/domains/change_domain.rb', line 92

def invert
  keys = inversion.select { |_, v| v.nil? }.keys.join(", ").presence
  errors = constraints.map(&:inversion_error).compact
  errors << "Can't invert #{keys}" if keys.present?
  raise IrreversibleMigration.new(self, nil, *errors) if errors.any?

  self.class.new(**to_h, **inversion)
end

#rename_constraint(name, to:) ⇒ Object



66
67
68
# File 'lib/pg_trunk/operations/domains/change_domain.rb', line 66

def rename_constraint(name, to:)
  constraints << Constraint.new(name: name, new_name: to)
end

#to_sql(_version) ⇒ Object



87
88
89
90
# File 'lib/pg_trunk/operations/domains/change_domain.rb', line 87

def to_sql(_version)
  [*change_default, *change_null, *change_constraints, *change_comment]
    .join(" ")
end

#validate_constraint(name) ⇒ Object



70
71
72
# File 'lib/pg_trunk/operations/domains/change_domain.rb', line 70

def validate_constraint(name)
  constraints << Constraint.new(name: name, valid: true)
end