Class: PGTrunk::Operations::ForeignKeys::AddForeignKey

Inherits:
Base show all
Defined in:
lib/pg_trunk/operations/foreign_keys/add_foreign_key.rb

Instance Method Summary collapse

Methods inherited from Base

#<=>

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

#invertObject



143
144
145
146
# File 'lib/pg_trunk/operations/foreign_keys/add_foreign_key.rb', line 143

def invert
  irreversible!("if_not_exists: true") if if_not_exists
  DropForeignKey.new(**to_h)
end

#to_sql(_version) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/pg_trunk/operations/foreign_keys/add_foreign_key.rb', line 134

def to_sql(_version)
  # Notice that in Rails the key `if_not_exists: true` means
  # the constraint should not be created if the table has ANY other
  # foreign key with the same reference <table>.
  return if if_not_exists && added?

  [add_constraint, create_comment, register_fk].join(" ")
end