class DBLeftovers::ForeignKey

Attributes

cascade[RW]
constraint_name[RW]
deferrable_initially_deferred[RW]
deferrable_initially_immediate[RW]
from_column[RW]
from_table[RW]
set_null[RW]
to_column[RW]
to_table[RW]

Public Class Methods

new(from_table, from_column, to_table, to_column, opts={}) click to toggle source
# File lib/db_leftovers/foreign_key.rb, line 6
def initialize(from_table, from_column, to_table, to_column, opts={})
  opts = {
    :deferrable => nil,
    :on_delete => nil,
    :name => name_constraint(from_table, from_column)
  }.merge(opts)
  opts.keys.each do |k|
    raise "`:set_null => true` should now be `:on_delete => :set_null`" if k.to_s == 'set_null'
    raise "`:cascade => true` should now be `:on_delete => :cascade`"   if k.to_s == 'cascade'
    raise "Unknown option: #{k}" unless [:on_delete, :name, :deferrable].include?(k)
  end
  raise "Unknown on_delete option: #{opts[:on_delete]}" unless [nil, :set_null, :cascade].include?(opts[:on_delete])
  raise "Unknown deferrable option: #{opts[:deferrable]}" unless [nil, :immediate, :deferred].include?(opts[:deferrable])
  @constraint_name = opts[:name].to_s
  @from_table = from_table.to_s
  @from_column = from_column.to_s
  @to_table = to_table.to_s
  @to_column = to_column.to_s

  @set_null = opts[:on_delete] == :set_null
  @cascade  = opts[:on_delete] == :cascade
  @deferrable_initially_immediate = opts[:deferrable] == :immediate
  @deferrable_initially_deferred  = opts[:deferrable] == :deferred

  raise "ON DELETE can't be both set_null and cascade" if @set_null and @cascade
  raise "DEFERRABLE can't be both immediate and deferred" if @deferrable_initially_immediate and @deferrable_initially_deferred
end

Public Instance Methods

equals(other) click to toggle source
# File lib/db_leftovers/foreign_key.rb, line 34
def equals(other)
  other.constraint_name == constraint_name and
  other.from_table == from_table and
  other.from_column == from_column and
  other.to_table == to_table and
  other.to_column == to_column and
  other.set_null == set_null and
  other.cascade == cascade and
  other.deferrable_initially_immediate == deferrable_initially_immediate and
  other.deferrable_initially_deferred == deferrable_initially_deferred
end
name_constraint(from_table, from_column) click to toggle source
# File lib/db_leftovers/foreign_key.rb, line 61
def name_constraint(from_table, from_column)
  "fk_#{from_table}_#{from_column}"
end
to_s() click to toggle source
# File lib/db_leftovers/foreign_key.rb, line 46
def to_s
  [
    "<#{@constraint_name}: from #{@from_table}.#{@from_column} to #{@to_table}.#{@to_column}",
    if @set_null; "ON DELETE SET NULL"
    elsif @cascade; "ON DELETE CASCADE"
    else; nil
    end,
    if @deferrable_initially_immediate; "DEFERRABLE INITIALLY IMMEDIATE"
    elsif @deferrable_initially_deferred; "DEFERRABLE INITIALLY DEFERRED"
    else; nil
    end,
    ">"
  ].compact.join(" ")
end