class PGTrunk::Operations::Domains::ChangeDomain

@private

Public Instance Methods

add_constraint(check, name: nil, valid: true) click to toggle source

Methods to populate ‘constraints` from the block

# 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) click to toggle source
# 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
invert() click to toggle source
# 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:) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/pg_trunk/operations/domains/change_domain.rb, line 70
def validate_constraint(name)
  constraints << Constraint.new(name: name, valid: true)
end

Private Instance Methods

change() click to toggle source
# File lib/pg_trunk/operations/domains/change_domain.rb, line 128
def change
  @change ||= {
    comment: comment,
    constraints: constraints.map(&:to_h).presence,
    default_sql: default_sql,
    null: null,
  }.compact
end
change_comment() click to toggle source
# File lib/pg_trunk/operations/domains/change_domain.rb, line 122
    def change_comment
      <<~SQL.squish if comment
        COMMENT ON DOMAIN #{name.to_sql} IS $comment$#{comment}$comment$;
      SQL
    end
change_constraints() click to toggle source
# File lib/pg_trunk/operations/domains/change_domain.rb, line 116
def change_constraints
  constraints.map do |c|
    "ALTER DOMAIN #{name.to_sql} #{c.to_sql};"
  end
end
change_default() click to toggle source
# File lib/pg_trunk/operations/domains/change_domain.rb, line 103
    def change_default
      <<~SQL.squish if default_sql
        ALTER DOMAIN #{name.to_sql}
        #{default_sql.present? ? "SET DEFAULT #{default_sql}" : 'DROP DEFAULT'};
      SQL
    end
change_null() click to toggle source
# File lib/pg_trunk/operations/domains/change_domain.rb, line 110
    def change_null
      <<~SQL.squish unless null.nil?
        ALTER DOMAIN #{name.to_sql} #{null ? 'DROP' : 'SET'} NOT NULL;
      SQL
    end
inversion() click to toggle source
# File lib/pg_trunk/operations/domains/change_domain.rb, line 137
def inversion
  @inversion ||= {
    comment: from_comment,
    constraints: constraints&.map(&:invert),
    default_sql: from_default_sql,
    null: !null,
  }.slice(*change.keys)
end