Class: DbDiff::Column
- Inherits:
-
TableElement
- Object
- TableElement
- DbDiff::Column
- Defined in:
- lib/dbdiff/column.rb
Instance Attribute Summary collapse
-
#auto_increment ⇒ Object
Returns the value of attribute auto_increment.
-
#column_type ⇒ Object
readonly
Returns the value of attribute column_type.
-
#data_type ⇒ Object
readonly
Returns the value of attribute data_type.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#not_null ⇒ Object
readonly
Returns the value of attribute not_null.
-
#numeric_precision ⇒ Object
readonly
Returns the value of attribute numeric_precision.
Attributes inherited from TableElement
Instance Method Summary collapse
- #==(other) ⇒ Object
- #add_delta ⇒ Object
- #definition(add_sql = false) ⇒ Object
- #drop_delta ⇒ Object
-
#initialize(info = {}) ⇒ Column
constructor
A new instance of Column.
- #modify_delta(new_element) ⇒ Object
Methods inherited from TableElement
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_increment ⇒ Object
Returns the value of attribute auto_increment.
4 5 6 |
# File 'lib/dbdiff/column.rb', line 4 def auto_increment @auto_increment end |
#column_type ⇒ Object (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_type ⇒ Object (readonly)
Returns the value of attribute data_type.
3 4 5 |
# File 'lib/dbdiff/column.rb', line 3 def data_type @data_type end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
3 4 5 |
# File 'lib/dbdiff/column.rb', line 3 def default @default end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
3 4 5 |
# File 'lib/dbdiff/column.rb', line 3 def length @length end |
#not_null ⇒ Object (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_precision ⇒ Object (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_delta ⇒ Object
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_delta ⇒ Object
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 |