Class: MyDiff::Change
- Inherits:
-
Object
- Object
- MyDiff::Change
- Defined in:
- lib/mydiff/change.rb
Overview
Represents one change to apply to the database
Instance Attribute Summary collapse
-
#chunks ⇒ Object
Returns the value of attribute chunks.
-
#type ⇒ Object
Can be one of
:new,:drop,:overwriteor:patch.
Instance Method Summary collapse
-
#add_chunk(type, pkey, fields = {}) ⇒ Object
Adds a chunk to apply in the change.
- #apply! ⇒ Object
- #delete_chunk(pkey) ⇒ Object
- #get_chunk(pkey) ⇒ Object
- #has_chunk?(pkey) ⇒ Boolean
-
#initialize(parent, type, table) ⇒ Change
constructor
Create a new change for a specific table.
Constructor Details
#initialize(parent, type, table) ⇒ Change
Create a new change for a specific table
parent-
the MyDiff parent class
type-
one of
:new,:drop,:overwriteor:patch table-
the table to change
15 16 17 18 19 20 |
# File 'lib/mydiff/change.rb', line 15 def initialize(parent, type, table) @md = parent @type = type @table = table @chunks = {} end |
Instance Attribute Details
#chunks ⇒ Object
Returns the value of attribute chunks.
8 9 10 |
# File 'lib/mydiff/change.rb', line 8 def chunks @chunks end |
#type ⇒ Object
Can be one of :new, :drop, :overwrite or :patch
7 8 9 |
# File 'lib/mydiff/change.rb', line 7 def type @type end |
Instance Method Details
#add_chunk(type, pkey, fields = {}) ⇒ Object
Adds a chunk to apply in the change.
type-
can be either
:new,:deleteor:change pkey-
should be a hash containing primary key fields
fields-
should be a hash containint the fields not belonging to primary key (only for
type=:newor:change)
27 28 29 30 |
# File 'lib/mydiff/change.rb', line 27 def add_chunk(type, pkey, fields = {}) raise ArgumentError, "Chunks can be added only if type is :patch" unless @type.eql?(:patch) @chunks[pkey.to_yaml] = { :type => type, :pkey => pkey, :fields => fields} end |
#apply! ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mydiff/change.rb', line 44 def apply! if @type.eql?(:new) puts "** Creating #{@table}" @md.select_old @md.my.query("CREATE TABLE #{@table} LIKE #{@md.newdb}.#{@table}") @md.my.query("INSERT INTO #{@table} SELECT * FROM #{@md.newdb}.#{@table}") elsif @type.eql?(:drop) puts "** Dropping table #{@table}" @md.select_old @md.my.query("DROP TABLE #{@table}") elsif @type.eql?(:overwrite) raise NotImplementedError, "Type :overwrite is not yet in use" elsif @type.eql?(:patch) printf "** Updating table #{@table}" @chunks.each_pair do |pkey, data| printf "." @md.select_old if data[:type].eql?(:new) field_list = [] data_list = [] data[:pkey].each_pair do |k,v| field_list << k data_list << "'" + @md.my.escape_string(v) + "'" end data[:fields].each_pair do |k,v| field_list << k data_list << "'" + @md.my.escape_string(v) + "'" end puts("INSERT INTO #{@table} (#{field_list.join(',')}) VALUES(#{data_list.join(',')})") if $DEBUG @md.my.query("INSERT INTO #{@table} (#{field_list.join(',')}) VALUES(#{data_list.join(',')})") elsif data[:type].eql?(:change) changes_list = [] pkey_list = [] data[:pkey].each_pair do |k,v| pkey_list << "#{k} = '" + @md.my.escape_string(v) + "'" end data[:fields].each_pair do |k,v| changes_list << "#{k} = '" + @md.my.escape_string(v) + "'" end puts("UPDATE #{@table} SET #{changes_list.join(',')} WHERE #{pkey_list.join(' AND ')}") if $DEBUG @md.my.query("UPDATE #{@table} SET #{changes_list.join(',')} WHERE #{pkey_list.join(' AND ')}") elsif data[:type].eql?(:delete) pkey_list = [] data[:pkey].each_pair do |k,v| pkey_list << "#{k} = '" + @md.my.escape_string(v) + "'" end puts("DELETE FROM #{@table} WHERE #{pkey_list.join(' AND ')}") if $DEBUG @md.my.query("DELETE FROM #{@table} WHERE #{pkey_list.join(' AND ')}") else raise NotImplementedError, "Invalid chunk type: #{data[:type]}" end end puts else raise NotImplementedError, "Invalid type #{@type.to_s}" end end |
#delete_chunk(pkey) ⇒ Object
32 33 34 |
# File 'lib/mydiff/change.rb', line 32 def delete_chunk(pkey) @chunks.delete(pkey.to_yaml) end |
#get_chunk(pkey) ⇒ Object
36 37 38 |
# File 'lib/mydiff/change.rb', line 36 def get_chunk(pkey) @chunks[pkey.to_yaml] end |
#has_chunk?(pkey) ⇒ Boolean
40 41 42 |
# File 'lib/mydiff/change.rb', line 40 def has_chunk?(pkey) @chunks.has_key?(pkey.to_yaml) end |