Class: Straitjacket::Constraint

Inherits:
Object
  • Object
show all
Defined in:
lib/straitjacket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, content, options) ⇒ Constraint

Returns a new instance of Constraint.



71
72
73
74
75
76
77
# File 'lib/straitjacket.rb', line 71

def initialize(table, content, options)
  @column = options[:column]
  @name = (options[:name] || default_name(table, options[:column])).to_s
  @table = table
  @content = content
  @options = options
end

Instance Attribute Details

#columnObject

Returns the value of attribute column.



69
70
71
# File 'lib/straitjacket.rb', line 69

def column
  @column
end

#contentObject

Returns the value of attribute content.



69
70
71
# File 'lib/straitjacket.rb', line 69

def content
  @content
end

#nameObject

Returns the value of attribute name.



69
70
71
# File 'lib/straitjacket.rb', line 69

def name
  @name
end

#optionsObject

Returns the value of attribute options.



69
70
71
# File 'lib/straitjacket.rb', line 69

def options
  @options
end

#sqlObject

Returns the value of attribute sql.



69
70
71
# File 'lib/straitjacket.rb', line 69

def sql
  @sql
end

#tableObject

Returns the value of attribute table.



69
70
71
# File 'lib/straitjacket.rb', line 69

def table
  @table
end

Instance Method Details

#apply(conn) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/straitjacket.rb', line 79

def apply(conn)
  conn.exec(sql)
rescue PGError => e
  if e.message =~ /already exists/
    conn.exec(%[ALTER TABLE "#{table}" DROP CONSTRAINT "#{name}"])
    retry
  else
    raise
  end
end