Class: Peegee::Constraint

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

Direct Known Subclasses

ForeignKey, PrimaryKey

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Constraint

Returns a new instance of Constraint.



4
5
6
7
8
# File 'lib/peegee/constraint.rb', line 4

def initialize(opts = {})
  @table_name = opts[:table_name]
  @constraint_name = opts[:constraint_name]
  @constraint_def = opts[:constraint_def]
end

Instance Method Details

#createObject

Creates this constraint definition.



16
17
18
19
# File 'lib/peegee/constraint.rb', line 16

def create
  sql = "alter table #{@table_name} add constraint #{@constraint_name} #{@constraint_def}"
  ActiveRecord::Base.connection.execute(sql)
end

#dropObject

Drops this constraint.



22
23
24
25
# File 'lib/peegee/constraint.rb', line 22

def drop
  sql = "alter table #{@table_name} drop constraint #{@constraint_name}"
  ActiveRecord::Base.connection.execute(sql)
end

#to_sObject

Returns human readable constraint.



11
12
13
# File 'lib/peegee/constraint.rb', line 11

def to_s
  "Constraint: #{@constraint_name} on #{table_name}"
end