Class: PGTrunk::Operations::Domains::Constraint

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

Overview

Definition for the domain's constraint

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(data) ⇒ Object



11
12
13
# File 'lib/pg_trunk/operations/domains/constraint.rb', line 11

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

Instance Method Details

#inversion_errorObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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

#invertObject



35
36
37
38
39
40
41
42
# 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

#optsObject



31
32
33
# File 'lib/pg_trunk/operations/domains/constraint.rb', line 31

def opts
  to_h.slice(:name)
end

#to_hObject



27
28
29
# File 'lib/pg_trunk/operations/domains/constraint.rb', line 27

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

#to_sqlObject



44
45
46
# File 'lib/pg_trunk/operations/domains/constraint.rb', line 44

def to_sql
  rename_sql || drop_sql || add_sql || validate_sql
end