Class: DbDiff::Column

Inherits:
TableElement show all
Defined in:
lib/dbdiff/column.rb

Instance Attribute Summary collapse

Attributes inherited from TableElement

#name, #table_name

Instance Method Summary collapse

Methods inherited from TableElement

#deep_clone

Constructor Details

#initialize(info = {}) ⇒ Column

Returns a new instance of Column.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dbdiff/column.rb', line 6

def initialize(info = {})
  super

  @name = info['COLUMN_NAME']
  @data_type = info['DATA_TYPE']
  @column_type = info['COLUMN_TYPE']
  @numeric_precision = info['NUMERIC_PRECISION']

  @default = (info['COLUMN_DEFAULT'] == 'NULL' ? nil : info['COLUMN_DEFAULT'])

  # XXX add type and length parsing
  @not_null = (info['IS_NULLABLE'] == 'NO' ? true : false)
  @auto_increment = (info['EXTRA'] == 'auto_increment' ? true : false)
end

Instance Attribute Details

#auto_incrementObject

Returns the value of attribute auto_increment.



4
5
6
# File 'lib/dbdiff/column.rb', line 4

def auto_increment
  @auto_increment
end

#column_typeObject (readonly)

Returns the value of attribute column_type.



3
4
5
# File 'lib/dbdiff/column.rb', line 3

def column_type
  @column_type
end

#data_typeObject (readonly)

Returns the value of attribute data_type.



3
4
5
# File 'lib/dbdiff/column.rb', line 3

def data_type
  @data_type
end

#defaultObject (readonly)

Returns the value of attribute default.



3
4
5
# File 'lib/dbdiff/column.rb', line 3

def default
  @default
end

#lengthObject (readonly)

Returns the value of attribute length.



3
4
5
# File 'lib/dbdiff/column.rb', line 3

def length
  @length
end

#not_nullObject (readonly)

Returns the value of attribute not_null.



3
4
5
# File 'lib/dbdiff/column.rb', line 3

def not_null
  @not_null
end

#numeric_precisionObject (readonly)

Returns the value of attribute numeric_precision.



3
4
5
# File 'lib/dbdiff/column.rb', line 3

def numeric_precision
  @numeric_precision
end

Instance Method Details

#==(other) ⇒ Object



60
61
62
63
64
65
# File 'lib/dbdiff/column.rb', line 60

def ==(other)
  attribs = %w(column_type default name not_null auto_increment)

  matches = attribs.find_all{|a|  self.send(a.to_sym) == other.send(a.to_sym)}
  matches.size == attribs.size
end

#add_deltaObject



40
41
42
43
44
45
46
# File 'lib/dbdiff/column.rb', line 40

def add_delta
  if self.auto_increment
    [Delta::AddColumn.new(self), Delta::ModifyColumnAddAI.new(self)]
  else
    Delta::AddColumn.new(self)
  end
end

#definition(add_sql = false) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/dbdiff/column.rb', line 49

def definition(add_sql = false)
 sql = "`%s` %s" % [self.name, self.column_type]

 sql += (@not_null ? ' NOT NULL' : ' NULL ')

 sql += (@default ?  " default '#{default}' " : '')
 sql += " auto_increment" if !add_sql && self.auto_increment

 sql
end

#drop_deltaObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dbdiff/column.rb', line 26

def drop_delta
  if self.auto_increment
    clone = self.deep_clone 
    clone.auto_increment = false

    # we have to change this to auto_increment = false
    # since its the primary key and we can't drop a primary key without disabling
    # auto_increment
    return [Delta::ModifyColumnRemoveAI.new(clone), Delta::DropColumn.new(clone)]
  else
    return Delta::DropColumn.new(self)
  end
end

#modify_delta(new_element) ⇒ Object



22
23
24
# File 'lib/dbdiff/column.rb', line 22

def modify_delta(new_element)
  Delta::ModifyColumn.new(new_element)
end