class PGTrunk::Operations::Domains::Constraint

@private Definition for the domain’s constraint

Public Class Methods

build(data) click to toggle source
# File lib/pg_trunk/operations/domains/constraint.rb, line 11
def self.build(data)
  data.is_a?(self) ? data : new(**data)
end

Public Instance Methods

inversion_error() click to toggle source
# File lib/pg_trunk/operations/domains/constraint.rb, line 48
    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 if check.present?

      "the constraint `#{name}` is dropped without `check` option."
    end
invert() click to toggle source
# File lib/pg_trunk/operations/domains/constraint.rb, line 35
def invert
  @invert ||= {}.tap do |i|
    i[:name] = new_name.presence || name
    i[:new_name] = name if new_name.present?
    i[:drop] = !drop if new_name.blank?
    i[:check] = check if drop
  end
end
opts() click to toggle source
# File lib/pg_trunk/operations/domains/constraint.rb, line 31
def opts
  to_h.slice(:name)
end
to_h() click to toggle source
# File lib/pg_trunk/operations/domains/constraint.rb, line 27
def to_h
  @to_h ||= attributes.compact.symbolize_keys
end
to_sql() click to toggle source
# File lib/pg_trunk/operations/domains/constraint.rb, line 44
def to_sql
  rename_sql || drop_sql || add_sql || validate_sql
end

Private Instance Methods

add_sql() click to toggle source
# File lib/pg_trunk/operations/domains/constraint.rb, line 82
    def add_sql
      <<~SQL.squish if check.present?
        ADD CONSTRAINT #{name.inspect}
        CHECK (#{check})#{' NOT VALID' unless valid}
      SQL
    end
drop_sql() click to toggle source
# File lib/pg_trunk/operations/domains/constraint.rb, line 72
def drop_sql
  return unless drop

  sql = "DROP CONSTRAINT"
  sql << " IF EXISTS" if if_exists
  sql << " #{name.inspect}"
  sql << " CASCADE" if force == :cascade
  sql << ";"
end
rename_sql() click to toggle source
# File lib/pg_trunk/operations/domains/constraint.rb, line 66
    def rename_sql
      <<~SQL.squish if new_name.present?
        RENAME CONSTRAINT #{name.inspect} TO #{new_name.inspect}
      SQL
    end
validate_sql() click to toggle source
# File lib/pg_trunk/operations/domains/constraint.rb, line 89
def validate_sql
  "VALIDATE CONSTRAINT #{name.inspect}" if valid
end