Class: DbDiff::Delta::ModifyRow

Inherits:
DbDiff::Delta show all
Defined in:
lib/dbdiff/delta.rb

Instance Attribute Summary

Attributes inherited from DbDiff::Delta

#element

Instance Method Summary collapse

Methods inherited from DbDiff::Delta

#initialize, #table, #to_a

Constructor Details

This class inherits a constructor from DbDiff::Delta

Instance Method Details

#process(database) ⇒ Object



93
94
95
96
97
98
# File 'lib/dbdiff/delta.rb', line 93

def process(database)
  table = table(database)
  old_row = table.rows.find{|r| r.name == element.name}
  table.rows.delete(old_row)
  table.rows << element 
end

#sqlObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dbdiff/delta.rb', line 75

def sql
  data = element.data
  where = element.primary_key.map {|k| "#{k}=" + Mysql.escape_string(data[k]) }.join(" AND ")
  sql = "UPDATE #{element.table_name} SET "

  updates = data.keys.map do |c|
    if data[c]
      " #{c}='" +  Mysql.escape_string(data[c]) + "'"
    else
      " #{c}= NULL"
    end
  end

  sql += updates.join(",") + " WHERE #{where}"

  return sql
end